From cde2c99e2d8e2b027b75062eb8aa0c5c2146d9ab Mon Sep 17 00:00:00 2001 From: ALEZ-DEV Date: Mon, 7 Apr 2025 17:58:50 +0200 Subject: [PATCH] now able to change the launch option --- babylonia-terminal-gui/src/ui/pages/game.rs | 1 + .../src/ui/pages/settings.rs | 104 +++++++++++++++--- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/babylonia-terminal-gui/src/ui/pages/game.rs b/babylonia-terminal-gui/src/ui/pages/game.rs index 9c60aaf..48743f0 100644 --- a/babylonia-terminal-gui/src/ui/pages/game.rs +++ b/babylonia-terminal-gui/src/ui/pages/game.rs @@ -131,6 +131,7 @@ impl SimpleAsyncComponent for GamePage { set_hexpand: false, set_width_request: 200, + #[watch] set_visible: model.game_state == GameState::GameNeedUpdate, #[watch] diff --git a/babylonia-terminal-gui/src/ui/pages/settings.rs b/babylonia-terminal-gui/src/ui/pages/settings.rs index 6fd6535..46a8405 100644 --- a/babylonia-terminal-gui/src/ui/pages/settings.rs +++ b/babylonia-terminal-gui/src/ui/pages/settings.rs @@ -1,13 +1,36 @@ +use arboard::Clipboard; +use babylonia_terminal_sdk::game_config::GameConfig; +use log::error; use relm4::{ - gtk::prelude::{OrientableExt, WidgetExt}, + gtk::{ + prelude::{EditableExt, GtkWindowExt, OrientableExt, WidgetExt}, + InputHints, + }, prelude::{gtk, AsyncComponentParts, SimpleAsyncComponent}, }; -pub struct SettingsPage; +use libadwaita::{ + self as adw, + prelude::{ + EntryRowExt, MessageDialogExt, PreferencesGroupExt, PreferencesPageExt, PreferencesRowExt, + }, +}; + +use crate::ui::MAIN_WINDOW; + +#[derive(Debug)] +pub enum SettingsPageMsg { + UpdateLaunchOption(Option), + ShowError(String), +} + +pub struct SettingsPage { + launch_option: String, +} #[relm4::component(pub, async)] impl SimpleAsyncComponent for SettingsPage { - type Input = (); + type Input = SettingsPageMsg; type Output = (); @@ -17,17 +40,29 @@ impl SimpleAsyncComponent for SettingsPage { gtk::Box { set_orientation: gtk::Orientation::Vertical, - gtk::Label { - set_label: "This page is under construction!", - add_css_class: "title-1", - }, + adw::PreferencesPage { + set_title: "General settings", - gtk::Label { - set_margin_top: 24, + add = &adw::PreferencesGroup { + set_width_request: 500, + set_title: "Launch option", + set_description: Some("Pass launch options to tinker the behavior of the game"), - set_label: "Please wait patiently :)", - add_css_class: "title-4", - }, + adw::EntryRow { + set_title: "%command%", + set_text: &model.launch_option, + + connect_changed[sender] => move |entry| { + let command = entry.text().trim().to_string(); + if command.is_empty() { + sender.input(SettingsPageMsg::UpdateLaunchOption(None)) + } else { + sender.input(SettingsPageMsg::UpdateLaunchOption(Some(command))) + } + } + } + } + } } } @@ -36,9 +71,52 @@ impl SimpleAsyncComponent for SettingsPage { root: Self::Root, sender: relm4::AsyncComponentSender, ) -> relm4::prelude::AsyncComponentParts { - let model = SettingsPage {}; + let launch_option = match GameConfig::get_launch_options().await { + Err(e) => String::new(), + Ok(v) => match v { + None => String::new(), + Some(launch) => launch, + }, + }; + + let model = SettingsPage { launch_option }; let widgets = view_output!(); AsyncComponentParts { model, widgets } } + + async fn update(&mut self, message: Self::Input, sender: relm4::AsyncComponentSender) { + match message { + SettingsPageMsg::UpdateLaunchOption(new_launch_option) => { + if let Err(e) = GameConfig::set_launch_options(new_launch_option).await { + sender.input(SettingsPageMsg::ShowError(format!( + "Something went wrong when updated the launch options : {}", + e + ))); + } + } + SettingsPageMsg::ShowError(message) => { + let dialog = unsafe { + adw::MessageDialog::new( + MAIN_WINDOW.as_ref(), + Some("Something went wrong"), + Some(&message), + ) + }; + + dialog.add_response("close", "Close"); + dialog.add_response("copy", "Copy"); + + dialog.set_response_appearance("copy", adw::ResponseAppearance::Suggested); + + dialog.connect_response(Some("copy"), move |_, _| { + if let Err(err) = Clipboard::new().unwrap().set_text(&message.clone()) { + error!("Failed to copy the error to the clipboard : {}", err); + } + }); + + dialog.present(); + } + } + } }