From 6815d8e01753f69845dd0bba7d052fa3e2cb942c Mon Sep 17 00:00:00 2001 From: scorpion-26 Date: Sat, 10 Jun 2023 23:42:41 +0200 Subject: [PATCH] Initialise GTK early --- src/AudioFlyin.cpp | 2 +- src/Bar.cpp | 2 +- src/BluetoothDevices.cpp | 2 +- src/Window.cpp | 36 +++++++++++++++++++++++++----------- src/Window.h | 12 ++++++++---- src/gBar.cpp | 5 +++-- 6 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/AudioFlyin.cpp b/src/AudioFlyin.cpp index ebd0785..73d667e 100644 --- a/src/AudioFlyin.cpp +++ b/src/AudioFlyin.cpp @@ -149,8 +149,8 @@ namespace AudioFlyin padding = Widget::Create(); mainWidget->AddChild(std::move(padding)); - window = Window(std::move(mainWidget), monitor); window.SetExclusive(false); window.SetAnchor(Anchor::Bottom); + window.SetMainWidget(std::move(mainWidget)); } } diff --git a/src/Bar.cpp b/src/Bar.cpp index 085db1d..1afd241 100644 --- a/src/Bar.cpp +++ b/src/Bar.cpp @@ -739,7 +739,7 @@ namespace Bar mainWidget->AddChild(std::move(time)); mainWidget->AddChild(std::move(right)); } - window = Window(std::move(mainWidget), monitor); window.SetAnchor(Anchor::Left | Anchor::Right | Anchor::Top); + window.SetMainWidget(std::move(mainWidget)); } } diff --git a/src/BluetoothDevices.cpp b/src/BluetoothDevices.cpp index 82c810a..6431c7e 100644 --- a/src/BluetoothDevices.cpp +++ b/src/BluetoothDevices.cpp @@ -298,10 +298,10 @@ namespace BluetoothDevices WidgetHeader(*mainWidget); WidgetBody(*mainWidget); - window = Window(std::move(mainWidget), monitor); window.SetExclusive(false); window.SetMargin(Anchor::Top, 8); window.SetAnchor(Anchor::Right | Anchor::Top); + window.SetMainWidget(std::move(mainWidget)); } } #endif diff --git a/src/Window.cpp b/src/Window.cpp index 333c193..c439954 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -8,7 +8,7 @@ #include #include -Window::Window(std::unique_ptr&& mainWidget, int32_t monitor) : m_MainWidget(std::move(mainWidget)), m_Monitor(monitor) {} +Window::Window(int32_t monitor) : m_MonitorID(monitor) {} 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); @@ -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); + 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); gtk_layer_init_for_window(m_Window); @@ -35,15 +52,7 @@ void Window::Run(int argc, char** argv) if (m_Exclusive) gtk_layer_auto_exclusive_zone_enable(m_Window); - GdkDisplay* defaultDisplay = gdk_display_get_default(); - 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); - } + gtk_layer_set_monitor(m_Window, m_Monitor); if (FLAG_CHECK(m_Anchor, Anchor::Left)) { @@ -100,6 +109,11 @@ void Window::UpdateMargin() } } +void Window::SetMainWidget(std::unique_ptr&& mainWidget) +{ + m_MainWidget = std::move(mainWidget); +} + void Window::SetMargin(Anchor anchor, int32_t margin) { if (FLAG_CHECK(anchor, Anchor::Left)) diff --git a/src/Window.h b/src/Window.h index e4b13f4..e834b42 100644 --- a/src/Window.h +++ b/src/Window.h @@ -16,12 +16,13 @@ class Window { public: Window() = default; - Window(std::unique_ptr&& mainWidget, int32_t monitor); + Window(int32_t monitor); Window(Window&& window) noexcept = default; Window& operator=(Window&& other) noexcept = default; ~Window(); - void Run(int argc, char** argv); + void Init(int argc, char** argv); + void Run(); void Close(); @@ -29,12 +30,14 @@ public: void SetMargin(Anchor anchor, int32_t margin); void SetExclusive(bool exclusive) { m_Exclusive = exclusive; } + void SetMainWidget(std::unique_ptr&& mainWidget); + private: void UpdateMargin(); void LoadCSS(GtkCssProvider* provider); - GtkWindow* m_Window; + GtkWindow* m_Window = nullptr; GtkApplication* m_App = nullptr; std::unique_ptr m_MainWidget; @@ -43,5 +46,6 @@ private: std::array, 4> m_Margin; bool m_Exclusive = true; - int32_t m_Monitor; + int32_t m_MonitorID; + GdkMonitor* m_Monitor = nullptr; }; diff --git a/src/gBar.cpp b/src/gBar.cpp index 7747976..5f244cc 100644 --- a/src/gBar.cpp +++ b/src/gBar.cpp @@ -41,7 +41,8 @@ int main(int argc, char** argv) monitor = atoi(argv[2]); } - Window window; + Window window(monitor); + window.Init(argc, argv); ASSERT(argc >= 2, "Too little arguments!"); if (strcmp(argv[1], "bar") == 0) { @@ -74,7 +75,7 @@ int main(int argc, char** argv) Plugin::LoadWidgetFromPlugin(argv[1], window, monitor); } - window.Run(argc, argv); + window.Run(); System::FreeResources(); if (flyin)