mirror of
https://github.com/ALEZ-DEV/Babylonia-terminal.git
synced 2025-12-16 17:38:51 +00:00
now able to change the launch option
This commit is contained in:
parent
c70feada75
commit
cde2c99e2d
@ -131,6 +131,7 @@ impl SimpleAsyncComponent for GamePage {
|
|||||||
set_hexpand: false,
|
set_hexpand: false,
|
||||||
set_width_request: 200,
|
set_width_request: 200,
|
||||||
|
|
||||||
|
#[watch]
|
||||||
set_visible: model.game_state == GameState::GameNeedUpdate,
|
set_visible: model.game_state == GameState::GameNeedUpdate,
|
||||||
|
|
||||||
#[watch]
|
#[watch]
|
||||||
|
|||||||
@ -1,13 +1,36 @@
|
|||||||
|
use arboard::Clipboard;
|
||||||
|
use babylonia_terminal_sdk::game_config::GameConfig;
|
||||||
|
use log::error;
|
||||||
use relm4::{
|
use relm4::{
|
||||||
gtk::prelude::{OrientableExt, WidgetExt},
|
gtk::{
|
||||||
|
prelude::{EditableExt, GtkWindowExt, OrientableExt, WidgetExt},
|
||||||
|
InputHints,
|
||||||
|
},
|
||||||
prelude::{gtk, AsyncComponentParts, SimpleAsyncComponent},
|
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<String>),
|
||||||
|
ShowError(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct SettingsPage {
|
||||||
|
launch_option: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[relm4::component(pub, async)]
|
#[relm4::component(pub, async)]
|
||||||
impl SimpleAsyncComponent for SettingsPage {
|
impl SimpleAsyncComponent for SettingsPage {
|
||||||
type Input = ();
|
type Input = SettingsPageMsg;
|
||||||
|
|
||||||
type Output = ();
|
type Output = ();
|
||||||
|
|
||||||
@ -17,17 +40,29 @@ impl SimpleAsyncComponent for SettingsPage {
|
|||||||
gtk::Box {
|
gtk::Box {
|
||||||
set_orientation: gtk::Orientation::Vertical,
|
set_orientation: gtk::Orientation::Vertical,
|
||||||
|
|
||||||
gtk::Label {
|
adw::PreferencesPage {
|
||||||
set_label: "This page is under construction!",
|
set_title: "General settings",
|
||||||
add_css_class: "title-1",
|
|
||||||
},
|
|
||||||
|
|
||||||
gtk::Label {
|
add = &adw::PreferencesGroup {
|
||||||
set_margin_top: 24,
|
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 :)",
|
adw::EntryRow {
|
||||||
add_css_class: "title-4",
|
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,
|
root: Self::Root,
|
||||||
sender: relm4::AsyncComponentSender<Self>,
|
sender: relm4::AsyncComponentSender<Self>,
|
||||||
) -> relm4::prelude::AsyncComponentParts<Self> {
|
) -> relm4::prelude::AsyncComponentParts<Self> {
|
||||||
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!();
|
let widgets = view_output!();
|
||||||
|
|
||||||
AsyncComponentParts { model, widgets }
|
AsyncComponentParts { model, widgets }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update(&mut self, message: Self::Input, sender: relm4::AsyncComponentSender<Self>) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user