Skip to content

Commit

Permalink
Add some more menubar code and other adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed May 11, 2024
1 parent cdb7fb5 commit fd441b2
Show file tree
Hide file tree
Showing 13 changed files with 422 additions and 19 deletions.
1 change: 1 addition & 0 deletions include/zwidget/widgets/imagebox/imagebox.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ImageBox : public Widget

void SetImage(std::shared_ptr<Image> newImage);

double GetPreferredWidth() const;
double GetPreferredHeight() const;

protected:
Expand Down
84 changes: 84 additions & 0 deletions include/zwidget/widgets/menubar/menubar.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,97 @@
#pragma once

#include "../../core/widget.h"
#include "../textlabel/textlabel.h"
#include "../imagebox/imagebox.h"

class Menu;
class MenubarItem;
class MenuItem;
class MenuItemSeparator;

class Menubar : public Widget
{
public:
Menubar(Widget* parent);
~Menubar();

MenubarItem* AddItem(std::string text, std::function<void(Menu* menu)> onOpen, bool alignRight = false);

protected:
void OnGeometryChanged() override;

private:
void ShowMenu(MenubarItem* item);
void CloseMenu();

std::vector<MenubarItem*> menuItems;
Menu* openMenu = nullptr;

friend class MenubarItem;
};

class MenubarItem : public Widget
{
public:
MenubarItem(Menubar* menubar, std::string text, bool alignRight);

void SetOpenCallback(std::function<void(Menu* menu)> callback) { onOpen = std::move(callback); }
const std::function<void(Menu* menu)>& GetOpenCallback() const { return onOpen; }

double GetPreferredWidth() const;

bool AlignRight = false;

protected:
void OnPaint(Canvas* canvas) override;
bool OnMouseDown(const Point& pos, InputKey key) override;
bool OnMouseUp(const Point& pos, InputKey key) override;
void OnMouseMove(const Point& pos) override;
void OnMouseLeave() override;

private:
Menubar* menubar = nullptr;
std::function<void(Menu* menu)> onOpen;
std::string text;
};

class Menu : public Widget
{
public:
Menu(Widget* parent);

void SetLeftPosition(const Point& globalPos);
void SetRightPosition(const Point& globalPos);
MenuItem* AddItem(std::shared_ptr<Image> icon, std::string text, std::function<void()> onClick = {});
MenuItemSeparator* AddSeparator();

double GetPreferredWidth() const;
double GetPreferredHeight() const;

protected:
void OnGeometryChanged() override;

std::function<void()> onCloseMenu;

friend class Menubar;
};

class MenuItem : public Widget
{
public:
MenuItem(Widget* parent);

ImageBox* icon = nullptr;
TextLabel* text = nullptr;

protected:
void OnMouseMove(const Point& pos) override;
void OnMouseLeave() override;
void OnGeometryChanged() override;
};

class MenuItemSeparator : public Widget
{
public:
MenuItemSeparator(Widget* parent);
};
5 changes: 4 additions & 1 deletion include/zwidget/window/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class DisplayWindowHost
class DisplayWindow
{
public:
static std::unique_ptr<DisplayWindow> Create(DisplayWindowHost* windowHost);
static std::unique_ptr<DisplayWindow> Create(DisplayWindowHost* windowHost, bool popupWindow);

static void ProcessEvents();
static void RunLoop();
Expand Down Expand Up @@ -157,6 +157,9 @@ class DisplayWindow
virtual int GetPixelHeight() const = 0;
virtual double GetDpiScale() const = 0;

virtual Point MapFromGlobal(const Point& pos) const = 0;
virtual Point MapToGlobal(const Point& pos) const = 0;

virtual void SetBorderColor(uint32_t bgra8) = 0;
virtual void SetCaptionColor(uint32_t bgra8) = 0;
virtual void SetCaptionTextColor(uint32_t bgra8) = 0;
Expand Down
36 changes: 36 additions & 0 deletions src/core/theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ DarkWidgetTheme::DarkWidgetTheme()
auto tabbar_tab = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "tabbar-tab");
auto tabwidget_stack = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "tabwidget-stack");
auto checkbox_label = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "checkbox-label");
auto menubaritem = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "menubaritem");
auto menu = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "menu");
auto menuitem = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "menuitem");

widget->SetString("font-family", "NotoSans");
widget->SetColor("color", Colorf::fromRgba8(226, 223, 219));
Expand Down Expand Up @@ -229,6 +232,21 @@ DarkWidgetTheme::DarkWidgetTheme()
checkbox_label->SetColor("checked-color", Colorf::fromRgba8(226, 223, 219));
checkbox_label->SetColor("unchecked-outer-border-color", Colorf::fromRgba8(99, 99, 99));
checkbox_label->SetColor("unchecked-inner-border-color", Colorf::fromRgba8(51, 51, 51));

menubaritem->SetColor("hover", "background-color", Colorf::fromRgba8(78, 78, 78));
menubaritem->SetColor("down", "background-color", Colorf::fromRgba8(88, 88, 88));

menu->SetDouble("noncontent-left", 5.0);
menu->SetDouble("noncontent-top", 5.0);
menu->SetDouble("noncontent-right", 5.0);
menu->SetDouble("noncontent-bottom", 5.0);
menu->SetColor("border-left-color", Colorf::fromRgba8(100, 100, 100));
menu->SetColor("border-top-color", Colorf::fromRgba8(100, 100, 100));
menu->SetColor("border-right-color", Colorf::fromRgba8(100, 100, 100));
menu->SetColor("border-bottom-color", Colorf::fromRgba8(100, 100, 100));

menuitem->SetColor("hover", "background-color", Colorf::fromRgba8(78, 78, 78));
menuitem->SetColor("down", "background-color", Colorf::fromRgba8(88, 88, 88));
}

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -246,6 +264,9 @@ LightWidgetTheme::LightWidgetTheme()
auto tabbar_tab = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "tabbar-tab");
auto tabwidget_stack = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "tabwidget-stack");
auto checkbox_label = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "checkbox-label");
auto menubaritem = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "menubaritem");
auto menu = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "menu");
auto menuitem = RegisterStyle(std::make_unique<BasicWidgetStyle>(widget), "menuitem");

widget->SetString("font-family", "NotoSans");
widget->SetColor("color", Colorf::fromRgba8(0, 0, 0));
Expand Down Expand Up @@ -321,4 +342,19 @@ LightWidgetTheme::LightWidgetTheme()
checkbox_label->SetColor("checked-color", Colorf::fromRgba8(50, 50, 50));
checkbox_label->SetColor("unchecked-outer-border-color", Colorf::fromRgba8(156, 156, 156));
checkbox_label->SetColor("unchecked-inner-border-color", Colorf::fromRgba8(200, 200, 200));

menubaritem->SetColor("hover", "background-color", Colorf::fromRgba8(200, 200, 200));
menubaritem->SetColor("down", "background-color", Colorf::fromRgba8(190, 190, 190));

menu->SetDouble("noncontent-left", 5.0);
menu->SetDouble("noncontent-top", 5.0);
menu->SetDouble("noncontent-right", 5.0);
menu->SetDouble("noncontent-bottom", 5.0);
menu->SetColor("border-left-color", Colorf::fromRgba8(155, 155, 155));
menu->SetColor("border-top-color", Colorf::fromRgba8(155, 155, 155));
menu->SetColor("border-right-color", Colorf::fromRgba8(155, 155, 155));
menu->SetColor("border-bottom-color", Colorf::fromRgba8(155, 155, 155));

menuitem->SetColor("hover", "background-color", Colorf::fromRgba8(200, 200, 200));
menuitem->SetColor("down", "background-color", Colorf::fromRgba8(190, 190, 190));
}
20 changes: 16 additions & 4 deletions src/core/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Widget::Widget(Widget* parent, WidgetType type) : Type(type)
{
if (type != WidgetType::Child)
{
DispWindow = DisplayWindow::Create(this);
DispWindow = DisplayWindow::Create(this, type == WidgetType::Popup);
DispCanvas = Canvas::create(DispWindow.get());
SetStyleState("root");

Expand Down Expand Up @@ -510,7 +510,7 @@ Point Widget::MapFromGlobal(const Point& pos) const
{
if (cur->DispWindow)
{
return p - cur->GetFrameGeometry().topLeft();
return cur->DispWindow->MapFromGlobal(p);
}
p -= cur->ContentGeometry.topLeft();
}
Expand All @@ -536,7 +536,7 @@ Point Widget::MapToGlobal(const Point& pos) const
{
if (cur->DispWindow)
{
return cur->GetFrameGeometry().topLeft() + p;
return cur->DispWindow->MapToGlobal(p);
}
p += cur->ContentGeometry.topLeft();
}
Expand Down Expand Up @@ -705,9 +705,21 @@ void Widget::OnWindowKeyUp(InputKey key)

void Widget::OnWindowGeometryChanged()
{
if (!DispWindow)
return;
Size size = DispWindow->GetClientSize();
FrameGeometry = Rect::xywh(0.0, 0.0, size.width, size.height);
ContentGeometry = FrameGeometry;

double left = FrameGeometry.left() + GetNoncontentLeft();
double top = FrameGeometry.top() + GetNoncontentTop();
double right = FrameGeometry.right() - GetNoncontentRight();
double bottom = FrameGeometry.bottom() - GetNoncontentBottom();
left = std::min(left, FrameGeometry.right());
top = std::min(top, FrameGeometry.bottom());
right = std::max(right, FrameGeometry.left());
bottom = std::max(bottom, FrameGeometry.top());
ContentGeometry = Rect::ltrb(left, top, right, bottom);

OnGeometryChanged();
}

Expand Down
8 changes: 8 additions & 0 deletions src/widgets/imagebox/imagebox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ ImageBox::ImageBox(Widget* parent) : Widget(parent)
{
}

double ImageBox::GetPreferredWidth() const
{
if (image)
return (double)image->GetWidth();
else
return 0.0;
}

double ImageBox::GetPreferredHeight() const
{
if (image)
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/lineedit/lineedit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <algorithm>

#include "widgets/lineedit/lineedit.h"
#include "core/utf8reader.h"
#include "core/colorf.h"
Expand Down
Loading

0 comments on commit fd441b2

Please sign in to comment.