Skip to content

Commit

Permalink
Most of the work for the dialog is done
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoneybun committed May 20, 2024
1 parent b196b09 commit 0133b2c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
7 changes: 7 additions & 0 deletions i18n/en/cosmic_backups.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ cosmic-backups = COSMIC Backups
app-title = COSMIC Backups
welcome = Backup and backup often! ✨
# Dialogs
create-repo = Create a new repository
save = Save
cancel = Cancel
confirm = Confirm
repo-location = Repository location
# Context Pages

## About
Expand Down
83 changes: 75 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::app::config::AppTheme;
use crate::fl;
use cosmic::app::{Command, Core};
use cosmic::app::{message, Command, Core};
use cosmic::iced::alignment::{Horizontal, Vertical};
use cosmic::iced::window;
use cosmic::iced_core::keyboard::Key;
Expand All @@ -17,7 +17,8 @@ use cosmic::{
ApplicationExt,
};
use cosmic::{widget, Application, Element};
use std::{collections::HashMap, env, process};
use std::collections::{HashMap, VecDeque};
use std::{env, process};

pub mod config;
pub mod menu;
Expand All @@ -29,10 +30,12 @@ pub struct App {
/// This is the core of your application, it is used to communicate with the Cosmic runtime.
/// It is used to send messages to your application, and to access the resources of the Cosmic runtime.
core: Core,
app_themes: Vec<String>,
config_handler: Option<cosmic_config::Config>,
config: config::CosmicBackupsConfig,
app_themes: Vec<String>,
context_page: ContextPage,
dialog_pages: VecDeque<DialogPage>,
dialog_text_input: widget::Id,
key_binds: HashMap<KeyBind, Action>,
}

Expand All @@ -42,8 +45,12 @@ pub struct App {
#[derive(Debug, Clone)]
pub enum Message {
// Cut(Option<Entity>),
DialogCancel,
DialogComplete,
DialogUpdate(DialogPage),
ToggleContextPage(ContextPage),
LaunchUrl(String),
OpenNewRepoDialog,
AppTheme(usize),
SystemThemeModeChange(cosmic_theme::ThemeMode),
NewRepo,
Expand All @@ -67,6 +74,11 @@ impl ContextPage {
}
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DialogPage {
New(String),
}

#[derive(Clone, Debug)]
pub struct Flags {
pub config_handler: Option<cosmic_config::Config>,
Expand All @@ -90,7 +102,7 @@ impl MenuAction for Action {
match self {
Action::About => Message::ToggleContextPage(ContextPage::About),
// Action::Cut => Message::Cut(entity_opt),
Action::NewRepo => Message::NewRepo,
Action::NewRepo => Message::OpenNewRepoDialog,
Action::NewSnap => Message::NewSnap,
Action::Settings => Message::ToggleContextPage(ContextPage::Settings),
Action::WindowClose => Message::WindowClose,
Expand Down Expand Up @@ -183,10 +195,12 @@ impl Application for App {
fn init(core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
let app = App {
core,
app_themes: vec![fl!("match-desktop"), fl!("dark"), fl!("light")],
context_page: ContextPage::Settings,
config_handler: flags.config_handler,
config: flags.config,
app_themes: vec![fl!("match-desktop"), fl!("dark"), fl!("light")],
dialog_pages: VecDeque::new(),
dialog_text_input: widget::Id::unique(),
key_binds: key_binds(),
};

Expand All @@ -204,6 +218,38 @@ impl Application for App {
})
}

fn dialog(&self) -> Option<Element<Message>> {
let dialog_page = match self.dialog_pages.front() {
Some(some) => some,
None => return None,
};

let spacing = cosmic::theme::active().cosmic().spacing;

let dialog = match dialog_page {
DialogPage::New(name) => widget::dialog(fl!("create-repo"))
.primary_action(
widget::button::suggested(fl!("save"))
.on_press_maybe(Some(Message::DialogComplete)),
)
.secondary_action(
widget::button::standard(fl!("cancel")).on_press(Message::DialogCancel),
)
.control(
widget::column::with_children(vec![
widget::text::body(fl!("repo-location")).into(),
widget::text_input("", name.as_str())
.id(self.dialog_text_input.clone())
.on_input(move |name| Message::DialogUpdate(DialogPage::New(name)))
.into(),
])
.spacing(spacing.space_xxs),
),
};

Some(dialog.into())
}

fn view(&self) -> Element<Self::Message> {
widget::container(widget::text::title1(fl!("welcome")))
.width(Length::Fill)
Expand Down Expand Up @@ -253,12 +299,29 @@ impl Application for App {
}
self.set_context_title(context_page.title());
}
Message::NewRepo => {
println!("created restic repository");
Message::OpenNewRepoDialog => {
self.dialog_pages.push_back(DialogPage::New(String::new()));
return widget::text_input::focus(self.dialog_text_input.clone());
}
Message::NewSnap => {
println!("snapshot saved");
}
Message::DialogComplete => {
if let Some(dialog_page) = self.dialog_pages.pop_front() {
match dialog_page {
DialogPage::New(name) => {
let list = List::new(&name);
commands.push(Command::perform(
todo::create_list(list, self.service.clone()),
|result| match result {
Ok(list) => message::app(Message::AddList(list)),
Err(_) => message::none(),
},
));
}
}
}
}
Message::WindowClose => {
return window::close(window::Id::MAIN);
}
Expand All @@ -272,7 +335,11 @@ impl Application for App {
Err(err) => {
eprintln!("failed to get current executable path: {}", err);
}
},
}
Message::DialogUpdate(dialog_page) => {
//TODO: panicless way to do this?
self.dialog_pages[0] = dialog_page;
}
Message::LaunchUrl(url) => match open::that_detached(&url) {
Ok(()) => {}
Err(err) => {
Expand Down

0 comments on commit 0133b2c

Please sign in to comment.