Skip to content

Commit

Permalink
add save keyboard/style as
Browse files Browse the repository at this point in the history
  • Loading branch information
justDeeevin committed Mar 26, 2024
1 parent bc7a82e commit da4c6d5
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/nuhxboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ pub struct NuhxBoard {
pub keyboard_properties_window_id: Option<window::Id>,
pub edit_history: Vec<Change>,
pub history_depth: usize,
pub save_keyboard_as_window_id: Option<window::Id>,
pub save_keyboard_as_category: String,
pub save_keyboard_as_name: String,
pub save_style_as_window_id: Option<window::Id>,
pub save_style_as_name: String,
pub save_style_as_global: bool,
}

#[derive(Default)]
Expand All @@ -62,6 +68,19 @@ pub enum StyleChoice {
Custom(String),
}

impl StyleChoice {
pub fn is_global(&self) -> bool {
matches!(self, StyleChoice::Global(_))
}

pub fn name(&self) -> String {
match self {
Self::Global(name) => name.clone(),
_ => self.to_string(),
}
}
}

impl std::fmt::Display for StyleChoice {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -97,7 +116,13 @@ pub enum Message {
OpenKeyboardProperties,
PushChange(Change),
Undo,
Redo
Redo,
OpenSaveKeyboardAs,
OpenSaveStyleAs,
ChangeSaveKeyboardAsCategory(String),
ChangeSaveKeyboardAsName(String),
ChangeSaveStyleAsName(String),
ToggleSaveStyleAsGlobal
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -220,6 +245,12 @@ impl Application for NuhxBoard {
keyboard_properties_window_id: None,
edit_history: vec![],
history_depth: 0,
save_keyboard_as_window_id: None,
save_keyboard_as_category: "".into(),
save_keyboard_as_name: "".into(),
save_style_as_window_id: None,
save_style_as_name: "".into(),
save_style_as_global: false,
},
Command::batch([
Command::perform(noop(), move |_| Message::ChangeKeyboardCategory(category)),
Expand All @@ -240,6 +271,10 @@ impl Application for NuhxBoard {
"Settings".to_owned()
} else if Some(window) == self.keyboard_properties_window_id {
"Keyboard Properties".to_owned()
} else if Some(window) == self.save_keyboard_as_window_id {
"Save Keyboard As".to_owned()
} else if Some(window) == self.save_style_as_window_id {
"Save Style As".to_owned()
} else {
unreachable!()
}
Expand Down Expand Up @@ -618,6 +653,7 @@ impl Application for NuhxBoard {
self.settings.category,
self.keyboard_options[self.keyboard.unwrap()]
)));
fs::create_dir_all(path.parent().unwrap()).unwrap();
let mut file = File::create(path).unwrap();
serde_json::to_writer_pretty(&mut file, &self.config).unwrap();
}
Expand Down Expand Up @@ -690,6 +726,46 @@ impl Application for NuhxBoard {
}
}
}
Message::OpenSaveKeyboardAs => {
let (id, command) = window::spawn(window::Settings {
size: iced::Size {
width: 400.0,
height: 100.0,
},
resizable: false,
..Default::default()
});
self.save_keyboard_as_window_id = Some(id);
self.save_keyboard_as_category.clone_from(&self.settings.category);
self.save_keyboard_as_name.clone_from(&self.keyboard_options[self.keyboard.unwrap()]);
return command;
}
Message::OpenSaveStyleAs => {
let (id, command) = window::spawn(window::Settings {
size: iced::Size {
width: 400.0,
height: 100.0,
},
resizable: false,
..Default::default()
});
self.save_style_as_window_id = Some(id);
self.save_style_as_name = self.style_options[self.style_choice.unwrap()].name();
self.save_style_as_global = self.style_options[self.style_choice.unwrap()].is_global();
return command;
}
Message::ChangeSaveKeyboardAsCategory(category) => {
self.save_keyboard_as_category = category;
}
Message::ChangeSaveKeyboardAsName(name) => {
self.save_keyboard_as_name = name;
}
Message::ChangeSaveStyleAsName(name) => {
self.save_style_as_name = name;
}
Message::ToggleSaveStyleAsGlobal => {
self.save_style_as_global = !self.save_style_as_global;
}
}
self.canvas.clear();
Command::none()
Expand All @@ -706,6 +782,10 @@ impl Application for NuhxBoard {
self.draw_settings_window()
} else if Some(window) == self.keyboard_properties_window_id {
self.draw_keyboard_properties_window()
} else if Some(window) == self.save_keyboard_as_window_id {
self.draw_save_keyboard_as_window()
} else if Some(window) == self.save_style_as_window_id {
self.draw_save_style_as_window()
} else {
unreachable!()
}
Expand Down
65 changes: 65 additions & 0 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,21 @@ impl NuhxBoard {
.style(iced::theme::Button::Custom(Box::new(WhiteButton {})))
.width(Length::Fixed(CONTEXT_MENU_WIDTH))
.into(),
button("Save Keyboard As...")
.on_press(Message::OpenSaveKeyboardAs)
.style(iced::theme::Button::Custom(Box::new(WhiteButton {})))
.width(Length::Fixed(CONTEXT_MENU_WIDTH))
.into(),
button("Save Style")
.on_press(Message::SaveStyle(None))
.style(iced::theme::Button::Custom(Box::new(WhiteButton {})))
.width(Length::Fixed(CONTEXT_MENU_WIDTH))
.into(),
button("Save Style As...")
.on_press(Message::OpenSaveStyleAs)
.style(iced::theme::Button::Custom(Box::new(WhiteButton {})))
.width(Length::Fixed(CONTEXT_MENU_WIDTH))
.into(),
]);
}

Expand Down Expand Up @@ -354,4 +364,59 @@ impl NuhxBoard {
]
.into()
}

pub fn draw_save_keyboard_as_window(&self) -> iced::Element<'_, Message, Theme, Renderer> {
column![
row![
text("Category: "),
text_input(
self.settings.category.as_str(),
&self.save_keyboard_as_category,
)
.on_input(Message::ChangeSaveKeyboardAsCategory)
],
row![
text("Name: "),
text_input(
&self.keyboard_options[self.keyboard.unwrap()],
&self.save_keyboard_as_name,
)
.on_input(Message::ChangeSaveKeyboardAsName)
],
button("Save").on_press(Message::SaveKeyboard(Some(
self.keyboards_path
.join(&self.save_keyboard_as_category)
.join(&self.save_keyboard_as_name)
.join("keyboard.json")
))),
]
.into()
}

pub fn draw_save_style_as_window(&self) -> iced::Element<'_, Message, Theme, Renderer> {
column![
row![
text("Name: "),
text_input(
&self.style_options[self.style_choice.unwrap()].name(),
&self.save_style_as_name,
)
.on_input(Message::ChangeSaveStyleAsName)
],
checkbox("Save as global", self.save_style_as_global)
.on_toggle(|_| Message::ToggleSaveStyleAsGlobal),
button("Save").on_press(Message::SaveStyle(Some(match self.save_style_as_global {
true => self
.keyboards_path
.join("global")
.join(format!("{}.style", &self.save_style_as_name)),
false => self
.keyboards_path
.join(&self.settings.category)
.join(&self.keyboard_options[self.keyboard.unwrap()])
.join(format!("{}.style", &self.save_style_as_name)),
}))),
]
.into()
}
}

0 comments on commit da4c6d5

Please sign in to comment.