Update to latest zwidget
This commit is contained in:
parent
22c788afc4
commit
d08b9b8568
23 changed files with 607 additions and 432 deletions
81
libraries/ZWidget/include/zwidget/core/theme.h
Normal file
81
libraries/ZWidget/include/zwidget/core/theme.h
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <unordered_map>
|
||||
#include "rect.h"
|
||||
#include "colorf.h"
|
||||
|
||||
class Widget;
|
||||
class Canvas;
|
||||
|
||||
class WidgetStyle
|
||||
{
|
||||
public:
|
||||
WidgetStyle(WidgetStyle* parentStyle = nullptr) : ParentStyle(parentStyle) { }
|
||||
virtual ~WidgetStyle() = default;
|
||||
virtual void Paint(Widget* widget, Canvas* canvas, Size size) = 0;
|
||||
|
||||
void SetBool(const std::string& state, const std::string& propertyName, bool value);
|
||||
void SetInt(const std::string& state, const std::string& propertyName, int value);
|
||||
void SetDouble(const std::string& state, const std::string& propertyName, double value);
|
||||
void SetString(const std::string& state, const std::string& propertyName, const std::string& value);
|
||||
void SetColor(const std::string& state, const std::string& propertyName, const Colorf& value);
|
||||
|
||||
void SetBool(const std::string& propertyName, bool value) { SetBool(std::string(), propertyName, value); }
|
||||
void SetInt(const std::string& propertyName, int value) { SetInt(std::string(), propertyName, value); }
|
||||
void SetDouble(const std::string& propertyName, double value) { SetDouble(std::string(), propertyName, value); }
|
||||
void SetString(const std::string& propertyName, const std::string& value) { SetString(std::string(), propertyName, value); }
|
||||
void SetColor(const std::string& propertyName, const Colorf& value) { SetColor(std::string(), propertyName, value); }
|
||||
|
||||
private:
|
||||
// Note: do not call these directly. Use widget->GetStyleXX instead since a widget may explicitly override a class style
|
||||
bool GetBool(const std::string& state, const std::string& propertyName) const;
|
||||
int GetInt(const std::string& state, const std::string& propertyName) const;
|
||||
double GetDouble(const std::string& state, const std::string& propertyName) const;
|
||||
std::string GetString(const std::string& state, const std::string& propertyName) const;
|
||||
Colorf GetColor(const std::string& state, const std::string& propertyName) const;
|
||||
|
||||
WidgetStyle* ParentStyle = nullptr;
|
||||
typedef std::variant<bool, int, double, std::string, Colorf> PropertyVariant;
|
||||
std::unordered_map<std::string, std::unordered_map<std::string, PropertyVariant>> StyleProperties;
|
||||
|
||||
const PropertyVariant* FindProperty(const std::string& state, const std::string& propertyName) const;
|
||||
|
||||
friend class Widget;
|
||||
};
|
||||
|
||||
class BasicWidgetStyle : public WidgetStyle
|
||||
{
|
||||
public:
|
||||
using WidgetStyle::WidgetStyle;
|
||||
void Paint(Widget* widget, Canvas* canvas, Size size) override;
|
||||
};
|
||||
|
||||
class WidgetTheme
|
||||
{
|
||||
public:
|
||||
virtual ~WidgetTheme() = default;
|
||||
|
||||
WidgetStyle* RegisterStyle(std::unique_ptr<WidgetStyle> widgetStyle, const std::string& widgetClass);
|
||||
WidgetStyle* GetStyle(const std::string& widgetClass);
|
||||
|
||||
static void SetTheme(std::unique_ptr<WidgetTheme> theme);
|
||||
static WidgetTheme* GetTheme();
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, std::unique_ptr<WidgetStyle>> Styles;
|
||||
};
|
||||
|
||||
class DarkWidgetTheme : public WidgetTheme
|
||||
{
|
||||
public:
|
||||
DarkWidgetTheme();
|
||||
};
|
||||
|
||||
class LightWidgetTheme : public WidgetTheme
|
||||
{
|
||||
public:
|
||||
LightWidgetTheme();
|
||||
};
|
||||
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <variant>
|
||||
#include <unordered_map>
|
||||
#include "canvas.h"
|
||||
#include "rect.h"
|
||||
#include "colorf.h"
|
||||
|
|
@ -39,16 +41,32 @@ public:
|
|||
|
||||
// Widget noncontent area
|
||||
void SetNoncontentSizes(double left, double top, double right, double bottom);
|
||||
double GetNoncontentLeft() const { return Noncontent.Left; }
|
||||
double GetNoncontentTop() const { return Noncontent.Top; }
|
||||
double GetNoncontentRight() const { return Noncontent.Right; }
|
||||
double GetNoncontentBottom() const { return Noncontent.Bottom; }
|
||||
double GetNoncontentLeft() const { return GetStyleDouble("noncontent-left"); }
|
||||
double GetNoncontentTop() const { return GetStyleDouble("noncontent-top"); }
|
||||
double GetNoncontentRight() const { return GetStyleDouble("noncontent-right"); }
|
||||
double GetNoncontentBottom() const { return GetStyleDouble("noncontent-bottom"); }
|
||||
|
||||
// Widget frame box
|
||||
Rect GetFrameGeometry() const;
|
||||
void SetFrameGeometry(const Rect& geometry);
|
||||
void SetFrameGeometry(double x, double y, double width, double height) { SetFrameGeometry(Rect::xywh(x, y, width, height)); }
|
||||
|
||||
// Style properties
|
||||
void SetStyleClass(const std::string& styleClass);
|
||||
const std::string& GetStyleClass() const { return StyleClass; }
|
||||
void SetStyleState(const std::string& state);
|
||||
const std::string& GetStyleState() const { return StyleState; }
|
||||
void SetStyleBool(const std::string& propertyName, bool value);
|
||||
void SetStyleInt(const std::string& propertyName, int value);
|
||||
void SetStyleDouble(const std::string& propertyName, double value);
|
||||
void SetStyleString(const std::string& propertyName, const std::string& value);
|
||||
void SetStyleColor(const std::string& propertyName, const Colorf& value);
|
||||
bool GetStyleBool(const std::string& propertyName) const;
|
||||
int GetStyleInt(const std::string& propertyName) const;
|
||||
double GetStyleDouble(const std::string& propertyName) const;
|
||||
std::string GetStyleString(const std::string& propertyName) const;
|
||||
Colorf GetStyleColor(const std::string& propertyName) const;
|
||||
|
||||
void SetWindowBackground(const Colorf& color);
|
||||
void SetWindowBorderColor(const Colorf& color);
|
||||
void SetWindowCaptionColor(const Colorf& color);
|
||||
|
|
@ -112,7 +130,7 @@ public:
|
|||
static Size GetScreenSize();
|
||||
|
||||
protected:
|
||||
virtual void OnPaintFrame(Canvas* canvas) { }
|
||||
virtual void OnPaintFrame(Canvas* canvas);
|
||||
virtual void OnPaint(Canvas* canvas) { }
|
||||
virtual bool OnMouseDown(const Point& pos, InputKey key) { return false; }
|
||||
virtual bool OnMouseDoubleclick(const Point& pos, InputKey key) { return false; }
|
||||
|
|
@ -167,14 +185,6 @@ private:
|
|||
|
||||
Colorf WindowBackground = Colorf::fromRgba8(240, 240, 240);
|
||||
|
||||
struct
|
||||
{
|
||||
double Left = 0.0;
|
||||
double Top = 0.0;
|
||||
double Right = 0.0;
|
||||
double Bottom = 0.0;
|
||||
} Noncontent;
|
||||
|
||||
std::string WindowTitle;
|
||||
std::unique_ptr<DisplayWindow> DispWindow;
|
||||
std::unique_ptr<Canvas> DispCanvas;
|
||||
|
|
@ -185,6 +195,11 @@ private:
|
|||
|
||||
StandardCursor CurrentCursor = StandardCursor::arrow;
|
||||
|
||||
std::string StyleClass = "widget";
|
||||
std::string StyleState;
|
||||
typedef std::variant<bool, int, double, std::string, Colorf> PropertyVariant;
|
||||
std::unordered_map<std::string, PropertyVariant> StyleProperties;
|
||||
|
||||
Widget(const Widget&) = delete;
|
||||
Widget& operator=(const Widget&) = delete;
|
||||
|
||||
|
|
|
|||
|
|
@ -69,7 +69,6 @@ public:
|
|||
std::function<void()> FuncEnterPressed;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
void OnMouseMove(const Point& pos) override;
|
||||
bool OnMouseDown(const Point& pos, InputKey key) override;
|
||||
|
|
|
|||
|
|
@ -24,7 +24,6 @@ public:
|
|||
|
||||
protected:
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
bool OnMouseDown(const Point& pos, InputKey key) override;
|
||||
bool OnMouseDoubleclick(const Point& pos, InputKey key) override;
|
||||
bool OnMouseWheel(const Point& pos, InputKey key) override;
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@ public:
|
|||
std::function<void()> OnClick;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
bool OnMouseDown(const Point& pos, InputKey key) override;
|
||||
bool OnMouseUp(const Point& pos, InputKey key) override;
|
||||
|
|
@ -30,6 +29,4 @@ protected:
|
|||
|
||||
private:
|
||||
std::string text;
|
||||
bool buttonDown = false;
|
||||
bool hot = false;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@ public:
|
|||
std::function<void()> OnCurrentChanged;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnGeometryChanged() override;
|
||||
|
||||
private:
|
||||
|
|
@ -67,7 +66,6 @@ public:
|
|||
std::function<void()> OnCurrentChanged;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnGeometryChanged() override;
|
||||
|
||||
private:
|
||||
|
|
@ -92,7 +90,6 @@ public:
|
|||
std::function<void()> OnClick;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnGeometryChanged() override;
|
||||
bool OnMouseDown(const Point& pos, InputKey key) override;
|
||||
bool OnMouseUp(const Point& pos, InputKey key) override;
|
||||
|
|
@ -116,7 +113,6 @@ public:
|
|||
Widget* GetCurrentWidget() const { return CurrentWidget; }
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnGeometryChanged() override;
|
||||
|
||||
private:
|
||||
|
|
|
|||
|
|
@ -53,7 +53,6 @@ public:
|
|||
std::function<void()> FuncEnterPressed;
|
||||
|
||||
protected:
|
||||
void OnPaintFrame(Canvas* canvas) override;
|
||||
void OnPaint(Canvas* canvas) override;
|
||||
void OnMouseMove(const Point& pos) override;
|
||||
bool OnMouseDown(const Point& pos, InputKey key) override;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue