Class: MittensUi::Application
- Inherits:
-
Object
- Object
- MittensUi::Application
- Defined in:
- lib/mittens_ui.rb
Overview
The main application class for MittensUi. Responsible for bootstrapping the GTK application, managing the window, layout, and providing access to shared application state like the persistent store.
Class Attribute Summary collapse
-
.app_id ⇒ String
readonly
The GTK application ID in reverse-domain format.
-
.gtk_app ⇒ Gtk::Application
readonly
The underlying Gtk::Application instance.
-
.layout ⇒ MittensUi::LayoutManager
readonly
The layout manager responsible for placing widgets in the window.
-
.window ⇒ Gtk::ApplicationWindow
readonly
The main application window.
Class Method Summary collapse
-
.apply_theme(theme) ⇒ void
Applies the theme based on the given mode.
-
.current_container ⇒ MittensUi::HBox?
Returns the currently active container, or nil if no container is active.
-
.current_theme ⇒ Symbol
Returns the current theme.
-
.exit { ... } ⇒ void
Exits the application cleanly, optionally running a block before quitting.
-
.pop_container ⇒ MittensUi::HBox?
Pops the most recently pushed container off the stack, restoring the previous container (or the main layout if the stack is empty) as the active render target.
-
.push_container(container) ⇒ Array
Pushes a container onto the container stack, making it the active container for widget rendering.
-
.store ⇒ MittensUi::Store
Returns the persistent store for this application.
-
.toggle_theme ⇒ void
Toggles between dark and light mode at runtime.
-
.Window(options = {}) {|window| ... } ⇒ void
Creates and runs the main application window.
Class Attribute Details
.app_id ⇒ String (readonly)
The GTK application ID in reverse-domain format.
67 68 69 |
# File 'lib/mittens_ui.rb', line 67 def app_id @app_id end |
.gtk_app ⇒ Gtk::Application (readonly)
The underlying Gtk::Application instance.
55 56 57 |
# File 'lib/mittens_ui.rb', line 55 def gtk_app @gtk_app end |
.layout ⇒ MittensUi::LayoutManager (readonly)
The layout manager responsible for placing widgets in the window.
59 60 61 |
# File 'lib/mittens_ui.rb', line 59 def layout @layout end |
.window ⇒ Gtk::ApplicationWindow (readonly)
The main application window.
63 64 65 |
# File 'lib/mittens_ui.rb', line 63 def window @window end |
Class Method Details
.apply_theme(theme) ⇒ void
This method returns an undefined value.
Applies the theme based on the given mode. Respects the system preference when set to :system.
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mittens_ui.rb', line 115 def apply_theme(theme) settings = Gtk::Settings.default case theme when :dark settings.gtk_application_prefer_dark_theme = true when :light begin settings.reset_property('gtk-application-prefer-dark-theme') settings.gtk_application_prefer_dark_theme = false rescue settings.gtk_application_prefer_dark_theme = false end when :system begin settings.reset_property('gtk-application-prefer-dark-theme') rescue settings.gtk_application_prefer_dark_theme = false end end @current_theme = theme @window&.queue_draw end |
.current_container ⇒ MittensUi::HBox?
Returns the currently active container, or nil if no container is active. When non-nil, Core#render will add newly created widgets to this container instead of the main layout grid. Supports nested HBox instances by always returning the innermost active container.
106 107 108 |
# File 'lib/mittens_ui.rb', line 106 def current_container @container_stack&.last end |
.current_theme ⇒ Symbol
Returns the current theme.
141 142 143 |
# File 'lib/mittens_ui.rb', line 141 def current_theme @current_theme || :system end |
.exit { ... } ⇒ void
This method returns an undefined value.
Exits the application cleanly, optionally running a block before quitting. If the block raises an error, the application exits with code 1. Always quits the GTK application loop via #gtk_app.
187 188 189 190 191 192 193 194 195 196 |
# File 'lib/mittens_ui.rb', line 187 def exit(&block) begin block.call if block_given? rescue => e puts("Exiting with: #{e}") Kernel.exit(1) ensure gtk_app&.quit end end |
.pop_container ⇒ MittensUi::HBox?
Pops the most recently pushed container off the stack, restoring the previous container (or the main layout if the stack is empty) as the active render target. Called automatically by HBox after its block finishes evaluating.
92 93 94 95 |
# File 'lib/mittens_ui.rb', line 92 def pop_container @container_stack ||= [] @container_stack.pop end |
.push_container(container) ⇒ Array
Pushes a container onto the container stack, making it the active container for widget rendering. Any widgets created after this call will render into this container instead of the main layout grid. Used internally by HBox to support block-style widget creation and nested HBox instances.
79 80 81 82 |
# File 'lib/mittens_ui.rb', line 79 def push_container(container) @container_stack ||= [] @container_stack.push(container) end |
.store ⇒ MittensUi::Store
Returns the persistent store for this application. The store is lazily initialized on first access and scoped to the application name.
206 207 208 |
# File 'lib/mittens_ui.rb', line 206 def store @store ||= Store.new(@app_id) end |
.toggle_theme ⇒ void
This method returns an undefined value.
Toggles between dark and light mode at runtime.
150 151 152 153 |
# File 'lib/mittens_ui.rb', line 150 def toggle_theme new_theme = current_theme == :dark ? :light : :dark apply_theme(new_theme) end |
.Window(options = {}) {|window| ... } ⇒ void
This method returns an undefined value.
Creates and runs the main application window. This is the entry point for every MittensUi application. The block is evaluated inside the GTK activate signal, so all widget creation should happen inside it.
175 176 177 |
# File 'lib/mittens_ui.rb', line 175 def Window( = {}, &block) init_gtk_application(, &block) end |