Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advanced/Settings Instance Button #358

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions src/advanced_frame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use iced::{Text, Element, Column, Button, button, Length, Alignment, alignment, Row, text_input, Padding};

use crate::{Message, instance::Instance, style};

#[derive(Debug, Clone)]
pub enum AdvancedMessage {
NameTextInputChanged(String),
ArgsTextInputChanged(String),
}

#[derive(Debug)]
pub struct AdvancedFrame {
old_name: String,
instance: Option<Instance>,

close_button: button::State,
name_input: text_input::State,
args_input: text_input::State
}

impl Default for AdvancedFrame {
fn default() -> Self {
Self {
old_name: String::default(),
instance: None,
close_button: button::State::default(),
name_input: text_input::State::default(),
args_input: text_input::State::default()
}
}
}

impl AdvancedFrame {
pub fn new(new_instance: Instance) -> Self {
Self {
old_name: String::from(&new_instance.name),
instance: Some(new_instance),
..Default::default()
}
}

pub fn update(&mut self, message: AdvancedMessage) {
match message {
AdvancedMessage::NameTextInputChanged(string) => {
self.instance.as_mut().unwrap().name = string;
}

AdvancedMessage::ArgsTextInputChanged(string) => {
self.instance.as_mut().unwrap().args = string;
}
}
}

pub fn view(&mut self) -> Element<Message> {

let close_button = Button::new(&mut self.close_button, style::close_icon())
.style(style::Button::Icon)
.on_press(match &self.instance {
Some(inst) => Message::CloseAdvanced(self.old_name.clone(), inst.clone()),
None => Message::Dummy(()),
});

let mut p = Padding::new(0);
p.bottom = 60;

let out = Column::new()
.push(
Row::new()
.width(Length::Fill)
.align_items(Alignment::End)
.push(
Text::new("Advanced Settings")
.size(26)
.horizontal_alignment(alignment::Horizontal::Center)
.width(Length::Fill),
)
.push(close_button)
.padding(p)
)
.padding(40)
.width(iced::Length::FillPortion(3))
.spacing(6);

if let Some(instance) = &self.instance {

let name_input = text_input::TextInput::new(
&mut self.name_input,
"Instance name",
&instance.name,
name_text_input_changed
).padding(4);

let out = out
.push(
Row::new()
.push(
{
let text = format!("Instance name:");
Text::new(text)
.size(18)
}
)
.push(
name_input
)
.spacing(14)
.align_items(Alignment::Center)
)
.push(
text_input::TextInput::new(
&mut self.args_input,
"Executable arguments",
&instance.args,
args_text_input_changed
).padding(4)
);

out
.push(
Text::new("-h or --help to log help")
)
.into()
} else {
out
.push(Text::new("How in the..."))
.into()
}
}
}

fn name_text_input_changed(string: String) -> Message {
Message::AdvancedMessage(AdvancedMessage::NameTextInputChanged(string))
}

fn args_text_input_changed(string: String) -> Message {
Message::AdvancedMessage(AdvancedMessage::ArgsTextInputChanged(string))
}
1 change: 1 addition & 0 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub fn install(
Ok(Instance::new(
destination,
executable_path,
String::new(),
name,
version,
instance_type,
Expand Down
29 changes: 24 additions & 5 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,16 @@ pub struct Instance {
#[serde(skip)]
folder_button: button::State,
#[serde(skip)]
advanced_button: button::State,
#[serde(skip)]
delete_button: button::State,

#[serde(skip)]
pub state: InstanceState,

pub path: PathBuf,
pub executable: PathBuf,
pub args: String,
pub name: String,
pub version: String,
pub instance_type: InstanceType,
Expand Down Expand Up @@ -149,6 +152,7 @@ pub enum InstanceMessage {
Play(bool),
Update,
Folder,
Advanced,
Delete,
StateChanged(InstanceState),
}
Expand All @@ -157,6 +161,7 @@ impl Instance {
pub fn new(
path: PathBuf,
executable: PathBuf,
args: String,
name: String,
version: String,
instance_type: InstanceType,
Expand All @@ -166,6 +171,7 @@ impl Instance {
Self {
path,
executable,
args,
name,
version,
instance_type,
Expand All @@ -175,6 +181,7 @@ impl Instance {
play_button: button::State::default(),
update_button: button::State::default(),
folder_button: button::State::default(),
advanced_button: button::State::default(),
delete_button: button::State::default(),
}
}
Expand All @@ -198,6 +205,7 @@ impl Instance {
self.executable.clone(),
self.name.clone(),
do_debug,
self.args.clone()
),
move |()| {
Message::InstanceMessage(
Expand Down Expand Up @@ -225,6 +233,12 @@ impl Instance {
InstanceMessage::Folder => {
iced::Command::perform(open_folder(self.path.clone()), Message::Dummy)
}
InstanceMessage::Advanced => {
let name = self.name.clone();
iced::Command::perform(dummy(), move |_| {
Message::OpenAdvanced(name.clone())
})
}
InstanceMessage::Delete => {
let name = self.name.clone();
iced::Command::perform(delete(self.path.clone()), move |_| {
Expand All @@ -249,6 +263,9 @@ impl Instance {
let folder_button = Button::new(&mut self.folder_button, style::folder_icon())
.style(style::Button::Icon)
.on_press(InstanceMessage::Folder);
let advanced_button = Button::new(&mut self.advanced_button, style::advanced_icon())
.style(style::Button::Icon)
.on_press(InstanceMessage::Advanced);
let mut delete_button = Button::new(&mut self.delete_button, style::delete_icon())
.style(style::Button::Destructive);

Expand Down Expand Up @@ -317,6 +334,7 @@ impl Instance {
.push(play_button)
.push(update_button)
.push(folder_button)
.push(advanced_button)
.push(delete_button)
}
})
Expand All @@ -335,6 +353,7 @@ pub async fn perform_install(
send_message(Message::AddInstance(Instance::new(
path.clone(),
"provisional".into(),
String::new(),
name.clone(),
instance_source.identifier.clone(),
instance_type,
Expand Down Expand Up @@ -383,15 +402,15 @@ pub async fn perform_update(instance: Instance) {
}
}

pub async fn perform_play(path: PathBuf, executable: PathBuf, name: String, do_debug: bool) {
pub async fn perform_play(path: PathBuf, executable: PathBuf, name: String, do_debug: bool, args: String) {
send_message(Message::MusicMessage(MusicCommand::Pause));
if let Err(e) = play(path, executable, name, do_debug).await {
if let Err(e) = play(path, executable, name, do_debug, args).await {
error!("Failed to run game: {:#}", e);
}
send_message(Message::MusicMessage(MusicCommand::Play));
}

pub async fn play(path: PathBuf, executable: PathBuf, name: String, do_debug: bool) -> Result<()> {
pub async fn play(path: PathBuf, executable: PathBuf, name: String, do_debug: bool, args: String) -> Result<()> {
let mut log_path = path;
log_path.push("logs");
fs::create_dir_all(&log_path)?;
Expand All @@ -415,9 +434,9 @@ pub async fn play(path: PathBuf, executable: PathBuf, name: String, do_debug: bo

let mut cmd = Command::new(&executable);
let output = if do_debug {
cmd.arg("-d").output()
cmd.args(["-d", &args]).output()
} else {
cmd.output()
cmd.arg(args).output()
};
match output {
Ok(output) => {
Expand Down
39 changes: 39 additions & 0 deletions src/instances_frame_holder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use iced::Element;

use crate::advanced_frame;
use crate::instances_frame;
use crate::Message;

#[derive(Debug)]
pub enum AdvancedFrameOpen {
Open(advanced_frame::AdvancedFrame),
Closed
}

#[derive(Debug)]
pub struct InstanceFrameHolder {
pub instances_frame: instances_frame::InstancesFrame,
pub advanced_frame_open: AdvancedFrameOpen,
}

impl Default for InstanceFrameHolder {
fn default() -> Self {
InstanceFrameHolder {
instances_frame: instances_frame::InstancesFrame::default(),
advanced_frame_open: AdvancedFrameOpen::Closed
}
}
}

impl InstanceFrameHolder {
pub fn view(&mut self) -> Element<Message> {
match &mut self.advanced_frame_open {
AdvancedFrameOpen::Open(advanced_frame) => {
advanced_frame.view()
},
AdvancedFrameOpen::Closed => {
self.instances_frame.view()
}
}
}
}
Loading