Skip to content

Commit

Permalink
Refactor/various2 (#373)
Browse files Browse the repository at this point in the history
* GenioWindow.cpp: Removed unneeded headers.
Fix indentation

* GenioWindow: Move associating editor to project in _PostFileLoad()

* Header reordering

* Refactor StyledItem::DrawIcon to not need an icon bitmap.

Inside DrawIcon, retrieve the icon. Subclasses can do differently.
ProjectItem, for example, gets the icon from the IconCache.

* Style
  • Loading branch information
jackburton79 authored Apr 29, 2024
1 parent 2edaf1d commit 6b54e01
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 48 deletions.
20 changes: 18 additions & 2 deletions src/project/ProjectItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,8 @@ ProjectItem::DrawItem(BView* owner, BRect bounds, bool complete)
else
SetTextFontFace(B_REGULAR_FACE);

const BBitmap* icon = IconCache::GetIcon(GetSourceItem()->EntryRef());
float iconSize = be_control_look->ComposeIconSize(B_MINI_ICON).Height();
BRect iconRect = DrawIcon(owner, bounds, icon, iconSize);
BRect iconRect = DrawIcon(owner, bounds, iconSize);

// There's a TextControl for renaming
if (fTextControl != nullptr) {
Expand Down Expand Up @@ -271,6 +270,23 @@ ProjectItem::CommitRename()
}


/* virtual */
BRect
ProjectItem::DrawIcon(BView* owner, const BRect& itemBounds,
const float& iconSize)
{
BPoint iconStartingPoint(itemBounds.left + 4.0f,
itemBounds.top + (itemBounds.Height() - iconSize) / 2.0f);
const BBitmap* icon = IconCache::GetIcon(GetSourceItem()->EntryRef());
if (icon != nullptr) {
owner->SetDrawingMode(B_OP_ALPHA);
owner->DrawBitmapAsync(icon, iconStartingPoint);
}

return BRect(iconStartingPoint, BSize(iconSize, iconSize));
}


void
ProjectItem::_DestroyTextWidget()
{
Expand Down
3 changes: 3 additions & 0 deletions src/project/ProjectItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class ProjectItem : public StyledItem {
bool fOpenedInEditor;
BTextControl *fTextControl;

BRect DrawIcon(BView* owner, const BRect& bounds,
const float& iconSize) override;

void _DestroyTextWidget();
};

Expand Down
1 change: 0 additions & 1 deletion src/ui/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include <Url.h>
#include <Volume.h>

#include "ActionManager.h"
#include "ConfigManager.h"
#include "EditorContextMenu.h"
#include "EditorMessages.h"
Expand Down
34 changes: 13 additions & 21 deletions src/ui/GenioWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@
#include <string>

#include <Alert.h>
#include <Application.h>
#include <Architecture.h>
#include <Bitmap.h>
#include <Button.h>
#include <Catalog.h>
#include <CheckBox.h>
#include <Clipboard.h>
#include <ControlLook.h>
#include <DirMenu.h>
#include <FilePanel.h>
#include <IconUtils.h>
#include <LayoutBuilder.h>
#include <MenuBar.h>
#include <NodeInfo.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <PathFinder.h>
Expand All @@ -34,13 +31,11 @@
#include <Screen.h>
#include <StringFormat.h>
#include <StringItem.h>
#include <Clipboard.h>

#include "ActionManager.h"
#include "ConfigManager.h"
#include "ConfigWindow.h"
#include "ConsoleIOView.h"
#include "ConsoleIOThread.h"
#include "EditorKeyDownMessageFilter.h"
#include "EditorMouseWheelMessageFilter.h"
#include "EditorMessages.h"
Expand Down Expand Up @@ -74,7 +69,6 @@
#include "argv_split.h"



#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "GenioWindow"

Expand Down Expand Up @@ -346,10 +340,10 @@ GenioWindow::MessageReceived(BMessage* message)
case MSG_ESCAPE_KEY:
{
if (CurrentFocus() == fFindTextControl->TextView()) {
_FindGroupShow(false);
_FindGroupShow(false);
} else if (CurrentFocus() == fReplaceTextControl->TextView()) {
_ReplaceGroupShow(false);
fFindTextControl->MakeFocus(true);
_ReplaceGroupShow(false);
fFindTextControl->MakeFocus(true);
} else if (CurrentFocus() == fRunConsoleProgramText->TextView()) {
fRunConsoleProgramGroup->SetVisible(false);
fRunConsoleProgramText->MakeFocus(false);
Expand Down Expand Up @@ -1631,7 +1625,8 @@ GenioWindow::_DebugProject()
argv_split parser(fActiveProject->GetTarget().String());
parser.parse(fActiveProject->GetExecuteArgs().String());

return be_roster->Launch("application/x-vnd.Haiku-debugger", parser.getArguments().size() , parser.argv());
return be_roster->Launch("application/x-vnd.Haiku-debugger",
parser.getArguments().size() , parser.argv());
}


Expand Down Expand Up @@ -1779,11 +1774,6 @@ GenioWindow::_FileOpen(BMessage* msg)
// TODO: Move some other stuff into _PostFileLoad()
_PostFileLoad(editor);

// Assign the right project to the Editor
for (int32 cycleIndex = 0; cycleIndex < GetProjectBrowser()->CountProjects(); cycleIndex++) {
ProjectFolder* project = GetProjectBrowser()->ProjectAt(cycleIndex);
_TryAssociateEditorWithProject(editor, project);
}
// apply LSP edits
std::string edit = msg->GetString("edit", "");
if (!edit.empty())
Expand Down Expand Up @@ -1919,24 +1909,21 @@ status_t
GenioWindow::_FileSaveAs(int32 selection, BMessage* message)
{
entry_ref ref;
BString name;
status_t status;

if ((status = message->FindRef("directory", &ref)) != B_OK)
return status;
BString name;
if ((status = message->FindString("name", &name)) != B_OK)
return status;

BPath path(&ref);
path.Append(name);
BEntry entry(path.Path(), true);
entry_ref newRef;

if ((status = entry.GetRef(&newRef)) != B_OK)
return status;

Editor* editor = fTabManager->EditorAt(selection);

if (editor == nullptr) {
BString notification;
notification
Expand Down Expand Up @@ -1985,6 +1972,11 @@ void
GenioWindow::_PostFileLoad(Editor* editor)
{
if (editor != nullptr) {
// Assign the right project to the Editor
for (int32 cycleIndex = 0; cycleIndex < GetProjectBrowser()->CountProjects(); cycleIndex++) {
ProjectFolder* project = GetProjectBrowser()->ProjectAt(cycleIndex);
_TryAssociateEditorWithProject(editor, project);
}
editor->ApplySettings();
editor->GetLSPEditorWrapper()->RequestDocumentSymbols();
BMessage noticeMessage(MSG_NOTIFY_EDITOR_FILE_OPENED);
Expand Down Expand Up @@ -4341,7 +4333,7 @@ GenioWindow::_HandleConfigurationChanged(BMessage* message)
ActionManager::SetPressed(MSG_TOGGLE_SPACES_ENDINGS, same);
}

_CollapseOrExpandProjects();
_CollapseOrExpandProjects();

Editor* selected = fTabManager->SelectedEditor();
_UpdateWindowTitle(selected ? selected->FilePath().String() : nullptr);
Expand Down
5 changes: 1 addition & 4 deletions src/ui/GenioWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
* Copyright 2017..2018 A. Mosca <amoscaster@gmail.com>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef GenioWINDOW_H
#define GenioWINDOW_H
#pragma once

#include <ObjectList.h>
#include <String.h>
Expand Down Expand Up @@ -256,5 +255,3 @@ class GenioWindow : public BWindow {
};

extern GenioWindow *gMainWindow;

#endif //GenioWINDOW_H
5 changes: 3 additions & 2 deletions src/ui/ProjectBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@
#include <File.h>
#include <Looper.h>
#include <MenuItem.h>
#include <MessageRunner.h>
#include <Mime.h>
#include <NaturalCompare.h>
#include <Path.h>
#include <PopUpMenu.h>
#include <Window.h>
#include <MessageRunner.h>
#include <StringList.h>
#include <Window.h>

#include <cassert>
#include <cstdio>

Expand Down
31 changes: 14 additions & 17 deletions src/ui/StyledItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,7 @@ StyledItem::DrawItem(BView* owner, BRect bounds, bool complete)
iconRect.right = iconRect.left + 4;
if (fIconName != nullptr) {
iconSize = be_control_look->ComposeIconSize(B_MINI_ICON).Height();
BBitmap* icon = new BBitmap(BRect(iconSize - 1.0f), 0, B_RGBA32);
BString iconName = fIconName;
if (fIconFollowsTheme) {
const int32 kBrightnessBreakValue = 126;
rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
if (base.Brightness() >= kBrightnessBreakValue)
iconName.Prepend("light-");
else
iconName.Prepend("dark-");
}
GetVectorIcon(iconName.String(), icon);
iconRect = DrawIcon(owner, bounds, icon, iconSize);
delete icon;
iconRect = DrawIcon(owner, bounds, iconSize);
}
BPoint textPoint(iconRect.right + be_control_look->DefaultLabelSpacing(),
bounds.top + BaselineOffset());
Expand Down Expand Up @@ -183,16 +171,25 @@ StyledItem::DrawText(BView* owner, const char* text,
/* virtual */
BRect
StyledItem::DrawIcon(BView* owner, const BRect& itemBounds,
const BBitmap* icon, float &iconSize)
const float &iconSize)
{
BBitmap* icon = new BBitmap(BRect(iconSize - 1.0f), 0, B_RGBA32);
BString iconName = fIconName;
if (fIconFollowsTheme) {
const int32 kBrightnessBreakValue = 126;
rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
if (base.Brightness() >= kBrightnessBreakValue)
iconName.Prepend("light-");
else
iconName.Prepend("dark-");
}
GetVectorIcon(iconName.String(), icon);
BPoint iconStartingPoint(itemBounds.left + 4.0f,
itemBounds.top + (itemBounds.Height() - iconSize) / 2.0f);
if (icon != nullptr) {
owner->SetDrawingMode(B_OP_ALPHA);
// TODO: changed from "DrawBitmapAsync()":
// which caused weird drawing issues on the Outline view
// like disappearing icons. Investigate
owner->DrawBitmap(icon, iconStartingPoint);
delete icon;
}

return BRect(iconStartingPoint, BSize(iconSize, iconSize));
Expand Down
2 changes: 1 addition & 1 deletion src/ui/StyledItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class StyledItem : public BStringItem {

protected:
virtual BRect DrawIcon(BView* owner, const BRect& bounds,
const BBitmap* icon, float& iconSize);
const float& iconSize);
virtual void DrawText(BView* owner, const char* text,
const char* extraText, const BPoint& textPoint);
private:
Expand Down

0 comments on commit 6b54e01

Please sign in to comment.