aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs246
1 files changed, 139 insertions, 107 deletions
diff --git a/src/main.rs b/src/main.rs
index 41d3f68..e1d3bc9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -47,8 +47,9 @@ use relm4::{
Component, ComponentController, ComponentParts, ComponentSender, RelmApp, RelmWidgetExt,
SimpleComponent,
actions::{AccelsPlus, ActionablePlus, RelmAction, RelmActionGroup},
- gtk,
+ gtk, adw
};
+use adw::prelude::*;
use sourceview5::LanguageManager;
use sourceview5::prelude::*;
use stages::{ALL_STAGE_TYPES, LocalizedStageType};
@@ -88,105 +89,12 @@ macro_rules! register_action {
let action: RelmAction<$action_type> = RelmAction::new_stateless(move |_| {
sender.input($message);
});
- $group.add_action(action);
+ $group.add_action(action.clone());
$app.set_accelerators_for_action::<$action_type>($accelerators);
+ action
}};
}
-fn setup_actions(window: &gtk::Window, sender: &ComponentSender<AppModel>) {
- let application = relm4::main_application();
- let mut action_group = RelmActionGroup::<WindowActionGroup>::new();
-
- register_action!(
- action_group,
- application,
- sender,
- ZoomInAction,
- Action::ZoomIn,
- &["<Primary>equal", "<Primary>plus"]
- );
- register_action!(
- action_group,
- application,
- sender,
- ZoomOutAction,
- Action::ZoomOut,
- &["<Primary>minus"]
- );
- register_action!(
- action_group,
- application,
- sender,
- ChangeOrientationAction,
- Action::ChangeOrientation,
- &["<Primary>l"]
- );
- register_action!(
- action_group,
- application,
- sender,
- ExportImageAction,
- Action::ExportImage,
- &["<Primary>e"]
- );
- register_action!(
- action_group,
- application,
- sender,
- NewAction,
- Action::New,
- &["<Primary>n"]
- );
- register_action!(
- action_group,
- application,
- sender,
- SaveAction,
- Action::Save { close_after: false },
- &["<Primary>s"]
- );
- register_action!(
- action_group,
- application,
- sender,
- SaveAsAction,
- Action::SaveAs { close_after: false },
- &["<Primary><Shift>s"]
- );
- register_action!(
- action_group,
- application,
- sender,
- OpenAction,
- Action::Open,
- &["<Primary>o"]
- );
- register_action!(
- action_group,
- application,
- sender,
- CloseAction,
- Action::CloseWindow,
- &["<Primary>w"]
- );
- register_action!(
- action_group,
- application,
- sender,
- PreferencesAction,
- Action::ShowPreferences,
- &["<Primary>comma"]
- );
-
- 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(window);
-}
-
struct AppInit {
zoom: f64,
file_path: Option<PathBuf>,
@@ -211,12 +119,14 @@ struct AppModel {
modified: bool,
pending_load_events: RefCell<u32>,
initialized: bool,
- window: gtk::Window,
+ window: adw::Window,
preferences: preferences::UserPreferences,
preferences_window: Option<relm4::Controller<preferences::PreferencesWindow>>,
editor_css_provider: gtk::CssProvider,
templates_menu: gio::Menu,
main_menu: gio::Menu,
+ horizontal_layout_enabled: bool,
+ layout_action: RelmAction<ChangeOrientationAction>
}
impl AppModel {
@@ -487,7 +397,7 @@ impl SimpleComponent for AppModel {
view! {
#[root]
- gtk::Window {
+ adw::Window {
#[watch]
set_title: Some(&model.window_title()),
set_default_width: constants::DEFAULT_WINDOW_WIDTH,
@@ -497,10 +407,8 @@ impl SimpleComponent for AppModel {
set_orientation: gtk::Orientation::Vertical,
set_spacing: 0,
- gtk::HeaderBar {
- pack_start = &gtk::Box {
- set_orientation: gtk::Orientation::Horizontal,
-
+ adw::HeaderBar {
+ pack_start = &adw::ToolbarView {
gtk::Button::with_label(&match model.orientation {
gtk::Orientation::Horizontal => tr!("command.view.use_vertical_layout"),
_ => tr!("command.view.use_horizontal_layout")
@@ -510,11 +418,12 @@ impl SimpleComponent for AppModel {
gtk::Orientation::Horizontal => SPLIT_VERTICAL_REGULAR,
_ => SPLIT_HORIZONTAL_REGULAR
},
+ #[watch]
+ set_visible: model.horizontal_layout_enabled,
ActionablePlus::set_stateless_action::<ChangeOrientationAction>: &(),
}
},
- pack_end = &gtk::Box {
- set_orientation: gtk::Orientation::Horizontal,
+ pack_end = &adw::ToolbarView {
gtk::Button::with_label(&tr!("command.file.export")) {
set_icon_name: IMAGE_REGULAR,
@@ -540,7 +449,7 @@ impl SimpleComponent for AppModel {
gtk::Paned {
#[watch]
- set_orientation: model.orientation,
+ set_orientation: if model.horizontal_layout_enabled { model.orientation } else { gtk::Orientation::Vertical },
set_expand: true,
#[wrap(Some)]
@@ -623,8 +532,6 @@ impl SimpleComponent for AppModel {
let (source, initial_content, current_file) = Self::init_source_buffer(&init, &sender);
- setup_actions(&root, &sender);
-
let source_view = sourceview5::View::with_buffer(&source);
source_view.set_monospace(true);
@@ -648,6 +555,100 @@ impl SimpleComponent for AppModel {
let prefs = preferences::storage::load();
let (templates_menu, main_menu) = Self::init_main_menu(&prefs);
+ // Actions
+
+ let application = relm4::main_application();
+ let mut action_group = RelmActionGroup::<WindowActionGroup>::new();
+
+ register_action!(
+ action_group,
+ application,
+ sender,
+ ZoomInAction,
+ Action::ZoomIn,
+ &["<Primary>equal", "<Primary>plus"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ ZoomOutAction,
+ Action::ZoomOut,
+ &["<Primary>minus"]
+ );
+ let layout_action = register_action!(
+ action_group,
+ application,
+ sender,
+ ChangeOrientationAction,
+ Action::ChangeOrientation,
+ &["<Primary>l"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ ExportImageAction,
+ Action::ExportImage,
+ &["<Primary>e"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ NewAction,
+ Action::New,
+ &["<Primary>n"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ SaveAction,
+ Action::Save { close_after: false },
+ &["<Primary>s"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ SaveAsAction,
+ Action::SaveAs { close_after: false },
+ &["<Primary><Shift>s"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ OpenAction,
+ Action::Open,
+ &["<Primary>o"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ CloseAction,
+ Action::CloseWindow,
+ &["<Primary>w"]
+ );
+ register_action!(
+ action_group,
+ application,
+ sender,
+ PreferencesAction,
+ Action::ShowPreferences,
+ &["<Primary>comma"]
+ );
+
+ 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 mut model = AppModel {
orientation: gtk::Orientation::Horizontal,
stage_type_list,
@@ -672,6 +673,8 @@ impl SimpleComponent for AppModel {
editor_css_provider,
templates_menu,
main_menu,
+ horizontal_layout_enabled: true,
+ layout_action
};
model.apply_preferences();
@@ -697,6 +700,23 @@ 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);
ComponentParts { model, widgets }
}
@@ -764,6 +784,18 @@ impl SimpleComponent for AppModel {
Action::MarkInitialized => self.initialized = true,
+ // Layout Transition
+ Action::DisableHorizontalLayout => {
+ self.horizontal_layout_enabled = false;
+ self.layout_action.set_enabled(false);
+ },
+ Action::EnableHorizontalLayout => {
+ self.horizontal_layout_enabled = true;
+ self.layout_action.set_enabled(true);
+ },
+
+ // Preferences
+
Action::ShowPreferences => {
if self.preferences_window.is_none() {
let controller = preferences::PreferencesWindow::builder()