1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
use std::{self, path::PathBuf, process::Command, sync::Arc};

use gooey_core::{
    assets::Configuration,
    figures::{Pixels, Points},
    AnyWindowBuilder, AppContext, WindowConfiguration,
};
use gooey_rasterizer::winit::{event::ModifiersState, window::Theme};
use kludgine::app::OpenableWindow;
use platforms::target::{OS, TARGET_OS};

use crate::{
    core::{Frontend, Gooey, Transmogrifiers, Widget, WidgetStorage},
    frontends::{
        rasterizer::{events::InputEvent as GooeyInputEvent, Rasterizer},
        renderers::kludgine::{
            kludgine::{self, prelude::*},
            Kludgine,
        },
    },
    widgets::rasterized::{default_transmogrifiers, register_transmogrifiers},
};

/// Runs a `Kludgine`-based [`App`](crate::app::App) with `transmogrifiers` and
/// the root widget from `initializer`. Unless overriden by `transmogrifier`,
/// all widgets from [`gooey::widget`](crate::widgets) will use the built-in
/// transmogrifiers.
#[allow(clippy::missing_panics_doc)]
pub fn kludgine_main_with<W: Widget + Send + Sync>(
    transmogrifiers: Transmogrifiers<Rasterizer<Kludgine>>,
    mut initial_window: gooey_core::WindowBuilder<W>,
    context: AppContext,
) {
    kludgine_run(
        kludgine_app(transmogrifiers, &mut initial_window, context),
        initial_window.configuration,
    );
}

/// Runs a `Kludgine`-based [`App`](crate::app::App) with the root widget from
/// `initializer`. All widgets from [`gooey::widget`](crate::widgets) will be
/// usable. If you wish to use other widgets, use `browser_main_with` and
/// provide the transmogrifiers for the widgets you wish to use.
pub fn kludgine_main<W: Widget>(initial_window: gooey_core::WindowBuilder<W>, context: AppContext) {
    kludgine_main_with(default_transmogrifiers(), initial_window, context);
}

/// Returns an initialized frontend using the root widget returned from `initializer`.
pub fn kludgine_app(
    mut transmogrifiers: Transmogrifiers<Rasterizer<Kludgine>>,
    builder: &mut dyn AnyWindowBuilder,
    context: AppContext,
) -> Rasterizer<Kludgine> {
    register_transmogrifiers(&mut transmogrifiers);
    let transmogrifiers = Arc::new(transmogrifiers);
    let storage = WidgetStorage::new(context);
    let ui = Gooey::new(transmogrifiers.clone(), builder.build(&storage), storage);
    initialize_rasterizer(ui, transmogrifiers)
}

fn initialize_rasterizer(
    ui: Gooey<Rasterizer<Kludgine>>,
    transmogrifiers: Arc<Transmogrifiers<Rasterizer<Kludgine>>>,
) -> Rasterizer<Kludgine> {
    let mut ui = Rasterizer::<Kludgine>::new(ui, Configuration::default());
    ui.set_window_creator(move |context, builder| {
        let storage = WidgetStorage::new(context);
        let root = builder.build(&storage);
        let ui = Gooey::new(transmogrifiers.clone(), root, storage);
        let ui = initialize_rasterizer(ui, transmogrifiers.clone());
        GooeyWindow {
            ui,
            redrawer: None,
            window_config: builder.configuration(),
        }
        .open();
    });
    ui.gooey().process_widget_messages(&ui);
    ui
}

/// Runs an initialized frontend.
pub fn kludgine_run(ui: Rasterizer<Kludgine>, window_config: WindowConfiguration) {
    SingleWindowApplication::run(GooeyWindow {
        ui,
        redrawer: None,
        window_config,
    });
}

struct GooeyWindow {
    ui: Rasterizer<Kludgine>,
    redrawer: Option<RedrawRequester>,
    window_config: WindowConfiguration,
}

impl WindowCreator for GooeyWindow {
    fn window_title(&self) -> String {
        self.window_config
            .title
            .as_ref()
            .map_or("Gooey - Kludgine", String::as_str)
            .to_owned()
    }

    fn initial_system_theme(&self) -> Theme {
        // winit doesn't have a way on linux to detect dark mode
        if TARGET_OS == OS::Linux {
            gtk3_preferred_theme()
                .or_else(gtk2_theme)
                .unwrap_or(Theme::Light)
        } else {
            Theme::Light
        }
    }

    fn initial_position(&self) -> Option<Point<i32, Pixels>> {
        self.window_config.position
    }

    fn initial_size(&self) -> Size<u32, Points> {
        self.window_config.size
    }

    fn resizable(&self) -> bool {
        self.window_config.resizable
    }

    fn maximized(&self) -> bool {
        self.window_config.maximized
    }

    fn transparent(&self) -> bool {
        self.window_config.transparent
    }

    fn decorations(&self) -> bool {
        self.window_config.decorations
    }

    fn always_on_top(&self) -> bool {
        self.window_config.always_on_top
    }
}

fn gtk3_preferred_theme() -> Option<Theme> {
    let settings_path = if let Ok(xdg_config_home) = std::env::var("XDG_CONFIG_HOME") {
        PathBuf::from(xdg_config_home)
            .join("gtk-3.0")
            .join("settings.ini")
    } else if let Ok(home) = std::env::var("HOME") {
        PathBuf::from(home)
            .join(".config")
            .join("gtk-3.0")
            .join("settings.ini")
    } else {
        return None;
    };
    let file_contents = std::fs::read_to_string(&settings_path).ok()?;
    // TODO make this more forgiving to whitespace
    if file_contents.contains("gtk-application-prefer-dark-theme=true") {
        Some(Theme::Dark)
    } else {
        Some(Theme::Light)
    }
}

fn gtk2_theme() -> Option<Theme> {
    let result = Command::new("gsettings")
        .args(["get", "org.gnome.desktop.interface", "gtk-theme"])
        .output()
        .ok()?;
    if result.status.success() {
        let result = String::from_utf8(result.stdout).ok()?;
        // gsettings wraps the output in single quotes
        if result.trim().ends_with("-dark'") {
            Some(Theme::Dark)
        } else {
            Some(Theme::Light)
        }
    } else {
        None
    }
}

impl Window for GooeyWindow {
    fn initialize(
        &mut self,
        _scene: &Target,
        redrawer: RedrawRequester,
        window: WindowHandle,
    ) -> kludgine::Result<()>
    where
        Self: Sized,
    {
        self.redrawer = Some(redrawer.clone());
        self.ui.set_window(KludgineWindow { window });
        self.ui.set_refresh_callback(move || {
            redrawer.awaken();
        });
        Ok(())
    }

    fn render(
        &mut self,
        scene: &Target,
        status: &mut RedrawStatus,
        _window: WindowHandle,
    ) -> kludgine::Result<()> {
        self.ui.render(Kludgine::from(scene));
        self.ui.gooey().process_widget_messages(&self.ui);
        if self.ui.needs_redraw() {
            status.set_needs_redraw();
        } else if let Some(duration) = self.ui.duration_until_next_redraw() {
            status.estimate_next_frame(duration);
        }

        Ok(())
    }

    fn update(
        &mut self,
        _scene: &Target,
        status: &mut RedrawStatus,
        _window: WindowHandle,
    ) -> kludgine::Result<()> {
        self.ui.gooey().process_widget_messages(&self.ui);
        if self.ui.needs_redraw() {
            status.set_needs_redraw();
        } else if let Some(duration) = self.ui.duration_until_next_redraw() {
            status.estimate_next_frame(duration);
        }

        Ok(())
    }

    fn process_input(
        &mut self,
        input: InputEvent,
        status: &mut RedrawStatus,
        scene: &Target,
        _window: WindowHandle,
    ) -> kludgine::Result<()> {
        let input = match input.event {
            Event::Keyboard {
                scancode,
                key,
                state,
            } => {
                // When a keyboard event happens, refresh the modifiers.
                let mut modifiers = ModifiersState::default();
                let scene_modifiers = scene.modifiers_pressed();
                if scene_modifiers.alt {
                    modifiers |= ModifiersState::ALT;
                }
                if scene_modifiers.control {
                    modifiers |= ModifiersState::CTRL;
                }
                if scene_modifiers.operating_system {
                    modifiers |= ModifiersState::LOGO;
                }
                if scene_modifiers.shift {
                    modifiers |= ModifiersState::SHIFT;
                }
                self.ui.handle_event(
                    gooey_rasterizer::events::WindowEvent::ModifiersChanged(modifiers),
                    Kludgine::from(scene),
                );

                GooeyInputEvent::Keyboard {
                    scancode,
                    key,
                    state,
                }
            }
            Event::MouseButton { button, state } => GooeyInputEvent::MouseButton { button, state },
            Event::MouseMoved { position } => GooeyInputEvent::MouseMoved {
                position: position.map(|p| p.cast_unit()),
            },
            Event::MouseWheel { delta, touch_phase } => {
                GooeyInputEvent::MouseWheel { delta, touch_phase }
            }
        };
        let result = self.ui.handle_event(
            gooey_rasterizer::events::WindowEvent::Input(input),
            Kludgine::from(scene),
        );
        self.ui.gooey().process_widget_messages(&self.ui);
        if result.needs_redraw || self.ui.needs_redraw() {
            status.set_needs_redraw();
        }
        Ok(())
    }

    fn receive_character(
        &mut self,
        character: char,
        status: &mut RedrawStatus,
        scene: &Target,
        _window: WindowHandle,
    ) -> kludgine::app::Result<()>
    where
        Self: Sized,
    {
        let result = self.ui.handle_event(
            gooey_rasterizer::events::WindowEvent::ReceiveCharacter(character),
            Kludgine::from(scene),
        );
        self.ui.gooey().process_widget_messages(&self.ui);
        if result.needs_redraw || self.ui.needs_redraw() {
            status.set_needs_redraw();
        }
        Ok(())
    }
}

#[derive(Debug)]
struct KludgineWindow {
    window: WindowHandle,
}

impl gooey_core::Window for KludgineWindow {
    fn set_title(&self, title: &str) {
        self.window.set_title(title);
    }

    fn inner_size(&self) -> gooey_core::figures::Size<u32, gooey_core::Pixels> {
        self.window.inner_size()
    }

    fn set_inner_size(&self, new_size: gooey_core::figures::Size<u32, gooey_core::Pixels>) {
        self.window.set_inner_size(new_size);
    }

    fn set_outer_position(
        &self,
        new_position: gooey_core::figures::Point<i32, gooey_core::Pixels>,
    ) {
        self.window.set_outer_position(new_position);
    }

    fn inner_position(&self) -> gooey_core::figures::Point<i32, gooey_core::Pixels> {
        self.window.inner_position()
    }

    fn set_always_on_top(&self, always: bool) {
        self.window.set_always_on_top(always);
    }

    fn maximized(&self) -> bool {
        self.window.maximized()
    }

    fn set_maximized(&self, maximized: bool) {
        self.window.set_maximized(maximized);
    }

    fn set_minimized(&self, minimized: bool) {
        self.window.set_minimized(minimized);
    }

    fn close(&self) {
        self.window.request_close();
    }

    fn outer_position(&self) -> gooey_core::figures::Point<i32, gooey_core::Pixels> {
        self.inner_position()
    }
}