diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 130 |
1 files changed, 73 insertions, 57 deletions
diff --git a/src/main.rs b/src/main.rs index f661df9..0115d88 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ #![allow(unused_assignments)] mod actions; +mod components; mod constants; mod dialogs; mod file_registry; @@ -26,7 +27,6 @@ mod i18n; mod preferences; mod stages; mod ui_helpers; -mod components; mod icon_names { #![allow(clippy::doc_markdown)] // Generated code from relm4_icons_build @@ -38,11 +38,11 @@ use std::convert::identity; use std::path::PathBuf; use crate::actions::Action; +use adw::prelude::*; use cairo::ImageSurface; use gtk::glib; -use relm4::prelude::*; use relm4::actions::{AccelsPlus, RelmAction, RelmActionGroup}; -use adw::prelude::*; +use relm4::prelude::*; use sourceview5::LanguageManager; use sourceview5::prelude::*; @@ -51,8 +51,8 @@ use stages::LocalizedStageType; use wmap_parser::{Map, parse}; use wmap_renderer::{Configuration, StageType, render_to_surface}; -use components::header::{Header, HeaderInit}; use components::footer::{Footer, FooterInit}; +use components::header::{Header, HeaderInit}; /// Macro to reduce boilerplate when registering actions with accelerators. macro_rules! register_action { @@ -107,9 +107,9 @@ impl relm4::actions::ActionName for NewFromTemplateAction { const NAME: &'static str = "new-from-template"; } - struct AppInit { zoom: f64, + orientation: gtk::Orientation, file_path: Option<PathBuf>, initial_content: Option<String>, } @@ -237,6 +237,7 @@ impl AppModel { let controller = AppModel::builder() .launch(AppInit { zoom: 1.0, + orientation: gtk::Orientation::Horizontal, file_path, initial_content, }) @@ -250,6 +251,7 @@ impl AppModel { file_registry::store_controller(controller); } + // Initialize the buffer that will be used to hold and read the source code. fn init_source_buffer( init: &AppInit, sender: &ComponentSender<Self>, @@ -283,6 +285,24 @@ impl AppModel { (source, initial_content, current_file) } + // Initializes the Source View that shows the colorized text. + fn init_source_view(source: &sourceview5::Buffer) -> (sourceview5::View, gtk::CssProvider) { + let source_view = sourceview5::View::with_buffer(source); + source_view.set_monospace(true); + + let editor_css_provider = gtk::CssProvider::new(); + source_view.add_css_class("map-editor"); + if let Some(display) = gtk::gdk::Display::default() { + gtk::style_context_add_provider_for_display( + &display, + &editor_css_provider, + gtk::STYLE_PROVIDER_PRIORITY_APPLICATION, + ); + } + (source_view, editor_css_provider) + } + + // Initializes the drawing area that shows the rendered map. fn init_drawing_area( initial_content: &str, ) -> ( @@ -307,6 +327,28 @@ impl AppModel { (drawing_area, map, render_configuration, stage_type, surface) } + // Initializes the breakpoint logic + fn init_breakpoints(sender: &ComponentSender<Self>, root: &adw::Window) { + let breakpoint = adw::Breakpoint::new(adw::BreakpointCondition::new_length( + adw::BreakpointConditionLengthType::MinWidth, + 440.0, + adw::LengthUnit::Sp, + )); + { + let sender = sender.clone(); + breakpoint.connect_apply(move |_| { + sender.input(Action::EnableHorizontalLayout); + }); + } + + { + let sender = sender.clone(); + breakpoint.connect_unapply(move |_| { + sender.input(Action::DisableHorizontalLayout); + }); + } + root.add_breakpoint(breakpoint); + } } #[relm4::component] @@ -366,22 +408,8 @@ impl SimpleComponent for AppModel { root: Self::Root, sender: ComponentSender<Self>, ) -> ComponentParts<Self> { - let (source, initial_content, current_file) = Self::init_source_buffer(&init, &sender); - - let source_view = sourceview5::View::with_buffer(&source); - source_view.set_monospace(true); - - let editor_css_provider = gtk::CssProvider::new(); - source_view.add_css_class("map-editor"); - if let Some(display) = gtk::gdk::Display::default() { - gtk::style_context_add_provider_for_display( - &display, - &editor_css_provider, - gtk::STYLE_PROVIDER_PRIORITY_APPLICATION, - ); - } - + let (source_view, editor_css_provider) = Self::init_source_view(&source); let (drawing_area, map, render_configuration, stage_type, surface) = Self::init_drawing_area(&initial_content); @@ -391,7 +419,6 @@ impl SimpleComponent for AppModel { let preferences = preferences::storage::load(); - let orientation = gtk::Orientation::Horizontal; let horizontal_layout_enabled = false; // Actions @@ -418,14 +445,15 @@ impl SimpleComponent for AppModel { &["<Primary>l"] ); - let sender_clone = sender.clone(); - let action: RelmAction<NewFromTemplateAction> = - RelmAction::new_with_target_value(move |_, template_id| { - sender_clone.input(Action::NewFromTemplateById(template_id)); - }); - action_group.add_action(action); - action_group.register_for_widget(&root); - + { + let sender = sender.clone(); + let action: RelmAction<NewFromTemplateAction> = + RelmAction::new_with_target_value(move |_, template_id| { + sender.input(Action::NewFromTemplateById(template_id)); + }); + action_group.add_action(action); + action_group.register_for_widget(&root); + } // Child Components let header = Header::builder() @@ -433,19 +461,17 @@ impl SimpleComponent for AppModel { layout_action, custom_stages: preferences.custom_stages.clone(), templates: preferences.map_templates.clone(), - orientation, - horizontal_layout_enabled + orientation: init.orientation, + horizontal_layout_enabled, }) .forward(sender.input_sender(), identity); let footer = Footer::builder() - .launch(FooterInit { - zoom: init.zoom - }) + .launch(FooterInit { zoom: init.zoom }) .forward(sender.input_sender(), identity); let mut model = AppModel { - orientation, + orientation: init.orientation, stage_type, source, source_view: source_view.clone(), @@ -466,7 +492,7 @@ impl SimpleComponent for AppModel { editor_css_provider, horizontal_layout_enabled: true, header, - footer + footer, }; model.apply_preferences(); @@ -492,30 +518,16 @@ impl SimpleComponent for AppModel { let widgets = view_output!(); // Breakpoints for Smaller Layouts - let breakpoint = adw::Breakpoint::new( - adw::BreakpointCondition::new_length( - adw::BreakpointConditionLengthType::MinWidth, - 440.0, - adw::LengthUnit::Sp - ) - ); - let sender_clone = sender.clone(); - breakpoint.connect_apply(move |_| { - sender_clone.input(Action::EnableHorizontalLayout); - }); - let sender_clone = sender.clone(); - breakpoint.connect_unapply(move |_| { - sender_clone.input(Action::DisableHorizontalLayout); - }); - root.add_breakpoint(breakpoint); + Self::init_breakpoints(&sender, &root); ComponentParts { model, widgets } } fn update(&mut self, message: Self::Input, sender: ComponentSender<Self>) { match message { Action::ChangeOrientation => handlers::view::change_orientation(self), - Action::StageTypeSelected(stage_type) => handlers::view::stage_type_selected(self, stage_type), - + Action::StageTypeSelected(stage_type) => { + handlers::view::stage_type_selected(self, stage_type); + } Action::SourceChanged => { let pending = *self.pending_load_events.borrow(); if pending > 0 { @@ -576,13 +588,16 @@ impl SimpleComponent for AppModel { Action::MarkInitialized => self.initialized = true, // Layout Transition - Action::DisableHorizontalLayout => handlers::view::change_horizontal_layout(self, false), + Action::DisableHorizontalLayout => { + handlers::view::change_horizontal_layout(self, false); + } Action::EnableHorizontalLayout => handlers::view::change_horizontal_layout(self, true), // Preferences - Action::ShowPreferences => handlers::preferences::show(self, &sender), - Action::PreferencesChanged(preferences) => handlers::preferences::update(self, preferences), + Action::PreferencesChanged(preferences) => { + handlers::preferences::update(self, preferences); + } Action::PreferencesWindowClosed => handlers::preferences::close(self), Action::ReloadPreferences => handlers::preferences::reload(self), } @@ -604,6 +619,7 @@ fn main() { let application = RelmApp::new(constants::APP_ID); application.run::<AppModel>(AppInit { zoom: 1.0, + orientation: gtk::Orientation::Horizontal, file_path: None, initial_content, }); |