Initialise GTK early

This commit is contained in:
scorpion-26 2023-06-10 23:42:41 +02:00
parent 2a66f1d64c
commit 6815d8e017
6 changed files with 39 additions and 20 deletions

View file

@ -149,8 +149,8 @@ namespace AudioFlyin
padding = Widget::Create<Box>(); padding = Widget::Create<Box>();
mainWidget->AddChild(std::move(padding)); mainWidget->AddChild(std::move(padding));
window = Window(std::move(mainWidget), monitor);
window.SetExclusive(false); window.SetExclusive(false);
window.SetAnchor(Anchor::Bottom); window.SetAnchor(Anchor::Bottom);
window.SetMainWidget(std::move(mainWidget));
} }
} }

View file

@ -739,7 +739,7 @@ namespace Bar
mainWidget->AddChild(std::move(time)); mainWidget->AddChild(std::move(time));
mainWidget->AddChild(std::move(right)); mainWidget->AddChild(std::move(right));
} }
window = Window(std::move(mainWidget), monitor);
window.SetAnchor(Anchor::Left | Anchor::Right | Anchor::Top); window.SetAnchor(Anchor::Left | Anchor::Right | Anchor::Top);
window.SetMainWidget(std::move(mainWidget));
} }
} }

View file

@ -298,10 +298,10 @@ namespace BluetoothDevices
WidgetHeader(*mainWidget); WidgetHeader(*mainWidget);
WidgetBody(*mainWidget); WidgetBody(*mainWidget);
window = Window(std::move(mainWidget), monitor);
window.SetExclusive(false); window.SetExclusive(false);
window.SetMargin(Anchor::Top, 8); window.SetMargin(Anchor::Top, 8);
window.SetAnchor(Anchor::Right | Anchor::Top); window.SetAnchor(Anchor::Right | Anchor::Top);
window.SetMainWidget(std::move(mainWidget));
} }
} }
#endif #endif

View file

@ -8,7 +8,7 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <gtk-layer-shell.h> #include <gtk-layer-shell.h>
Window::Window(std::unique_ptr<Widget>&& mainWidget, int32_t monitor) : m_MainWidget(std::move(mainWidget)), m_Monitor(monitor) {} Window::Window(int32_t monitor) : m_MonitorID(monitor) {}
Window::~Window() Window::~Window()
{ {
@ -19,7 +19,7 @@ Window::~Window()
} }
} }
void Window::Run(int argc, char** argv) void Window::Init(int argc, char** argv)
{ {
gtk_init(&argc, &argv); gtk_init(&argc, &argv);
@ -28,6 +28,23 @@ void Window::Run(int argc, char** argv)
gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)CSS::GetProvider(), GTK_STYLE_PROVIDER_PRIORITY_USER); gtk_style_context_add_provider_for_screen(gdk_screen_get_default(), (GtkStyleProvider*)CSS::GetProvider(), GTK_STYLE_PROVIDER_PRIORITY_USER);
GdkDisplay* defaultDisplay = gdk_display_get_default();
ASSERT(defaultDisplay != nullptr, "Cannot get display!");
if (m_MonitorID != -1)
{
m_Monitor = gdk_display_get_monitor(defaultDisplay, m_MonitorID);
ASSERT(m_Monitor, "Cannot get monitor!");
}
else
{
m_Monitor = gdk_display_get_primary_monitor(defaultDisplay);
}
}
void Window::Run()
{
ASSERT(m_MainWidget, "Main Widget not set!");
m_Window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL); m_Window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_layer_init_for_window(m_Window); gtk_layer_init_for_window(m_Window);
@ -35,15 +52,7 @@ void Window::Run(int argc, char** argv)
if (m_Exclusive) if (m_Exclusive)
gtk_layer_auto_exclusive_zone_enable(m_Window); gtk_layer_auto_exclusive_zone_enable(m_Window);
GdkDisplay* defaultDisplay = gdk_display_get_default(); gtk_layer_set_monitor(m_Window, m_Monitor);
ASSERT(defaultDisplay != nullptr, "Cannot get display!");
GdkMonitor* selectedMon = nullptr;
if (m_Monitor != -1)
{
selectedMon = gdk_display_get_monitor(defaultDisplay, m_Monitor);
ASSERT(selectedMon != nullptr, "Cannot get monitor!");
gtk_layer_set_monitor(m_Window, selectedMon);
}
if (FLAG_CHECK(m_Anchor, Anchor::Left)) if (FLAG_CHECK(m_Anchor, Anchor::Left))
{ {
@ -100,6 +109,11 @@ void Window::UpdateMargin()
} }
} }
void Window::SetMainWidget(std::unique_ptr<Widget>&& mainWidget)
{
m_MainWidget = std::move(mainWidget);
}
void Window::SetMargin(Anchor anchor, int32_t margin) void Window::SetMargin(Anchor anchor, int32_t margin)
{ {
if (FLAG_CHECK(anchor, Anchor::Left)) if (FLAG_CHECK(anchor, Anchor::Left))

View file

@ -16,12 +16,13 @@ class Window
{ {
public: public:
Window() = default; Window() = default;
Window(std::unique_ptr<Widget>&& mainWidget, int32_t monitor); Window(int32_t monitor);
Window(Window&& window) noexcept = default; Window(Window&& window) noexcept = default;
Window& operator=(Window&& other) noexcept = default; Window& operator=(Window&& other) noexcept = default;
~Window(); ~Window();
void Run(int argc, char** argv); void Init(int argc, char** argv);
void Run();
void Close(); void Close();
@ -29,12 +30,14 @@ public:
void SetMargin(Anchor anchor, int32_t margin); void SetMargin(Anchor anchor, int32_t margin);
void SetExclusive(bool exclusive) { m_Exclusive = exclusive; } void SetExclusive(bool exclusive) { m_Exclusive = exclusive; }
void SetMainWidget(std::unique_ptr<Widget>&& mainWidget);
private: private:
void UpdateMargin(); void UpdateMargin();
void LoadCSS(GtkCssProvider* provider); void LoadCSS(GtkCssProvider* provider);
GtkWindow* m_Window; GtkWindow* m_Window = nullptr;
GtkApplication* m_App = nullptr; GtkApplication* m_App = nullptr;
std::unique_ptr<Widget> m_MainWidget; std::unique_ptr<Widget> m_MainWidget;
@ -43,5 +46,6 @@ private:
std::array<std::pair<Anchor, int32_t>, 4> m_Margin; std::array<std::pair<Anchor, int32_t>, 4> m_Margin;
bool m_Exclusive = true; bool m_Exclusive = true;
int32_t m_Monitor; int32_t m_MonitorID;
GdkMonitor* m_Monitor = nullptr;
}; };

View file

@ -41,7 +41,8 @@ int main(int argc, char** argv)
monitor = atoi(argv[2]); monitor = atoi(argv[2]);
} }
Window window; Window window(monitor);
window.Init(argc, argv);
ASSERT(argc >= 2, "Too little arguments!"); ASSERT(argc >= 2, "Too little arguments!");
if (strcmp(argv[1], "bar") == 0) if (strcmp(argv[1], "bar") == 0)
{ {
@ -74,7 +75,7 @@ int main(int argc, char** argv)
Plugin::LoadWidgetFromPlugin(argv[1], window, monitor); Plugin::LoadWidgetFromPlugin(argv[1], window, monitor);
} }
window.Run(argc, argv); window.Run();
System::FreeResources(); System::FreeResources();
if (flyin) if (flyin)