Skip to content

Commit

Permalink
Move page widget to widgets mod
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Feb 12, 2023
1 parent 3b925ee commit 328f21a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 56 deletions.
34 changes: 2 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
#![feature(let_chains)]
#![feature(concat_idents)]

use eframe::{
egui::{self, Layout, RichText, Ui, WidgetText},
emath::Align,
};
use egui::Color32;
use egui::emath::Align;
use egui::{self, Color32, Layout, RichText};
use receive::ReceiveView;
use send::SendView;
use widgets::{view_switcher, ViewSwitcher};
Expand Down Expand Up @@ -80,33 +77,6 @@ impl ViewSwitcher for PortalApp {
}
}

fn page<'a>(
ui: &mut Ui,
title: impl Into<RichText>,
text: impl Into<WidgetText>,
icon: impl Into<Option<&'a str>>,
) {
if let Some(icon) = icon.into() {
ui.label(RichText::new(icon).size(120.0));
}
ui.add_space(10.0);
ui.label(title.into().size(30.0).strong());
ui.add_space(10.0);
ui.label(text);
}

fn page_with_content<'a, T>(
ui: &mut Ui,
title: impl Into<RichText>,
text: impl Into<WidgetText>,
icon: impl Into<Option<&'a str>>,
add_contents: impl FnOnce(&mut Ui) -> T,
) -> T {
page(ui, title, text, icon);
ui.add_space(20.0);
add_contents(ui)
}

#[macro_export]
macro_rules! update {
($target:expr, $pattern:pat => $match_arm:expr) => {
Expand Down
26 changes: 10 additions & 16 deletions src/receive.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::egui_ext::ContextExt;
use crate::transmit_info::transit_info_message;
use crate::widgets::{cancel_button, CancelLabel};
use crate::widgets::{cancel_button, page, page_with_content, CancelLabel};
use crate::{
error::PortalError,
fs::{persist_temp_file, persist_with_conflict_resolution, sanitize_untrusted_filename},
Expand Down Expand Up @@ -108,7 +108,7 @@ impl ReceiveView {
controller.cancel();
}

crate::page_with_content(
page_with_content(
ui,
"Connecting with peer",
"Preparing to Receive File",
Expand All @@ -121,7 +121,7 @@ impl ReceiveView {
ReceiveState::Error(error) => {
let error = error.to_string();
self.back_button(ui);
crate::page(ui, "File Transfer Failed", error, "❌");
page(ui, "File Transfer Failed", error, "❌");
}
ReceiveState::Connected(ref receive_request) => {
match show_connected_page(ui, receive_request) {
Expand All @@ -148,7 +148,7 @@ impl ReceiveView {
}

match controller.transit_info() {
Some(transit_info) => crate::page_with_content(
Some(transit_info) => page_with_content(
ui,
"Receiving File",
transit_info_message(transit_info, filename.as_os_str()),
Expand All @@ -160,7 +160,7 @@ impl ReceiveView {
);
},
),
None => crate::page_with_content(
None => page_with_content(
ui,
"Connected to Peer",
format!("Preparing to receive file \"{}\"", filename.display()),
Expand All @@ -172,15 +172,9 @@ impl ReceiveView {
}
}
ReceiveState::Rejecting(_) => {
crate::page_with_content(
ui,
"Receive File",
"Rejecting File Transfer",
"📥",
|ui| {
ui.spinner();
},
);
page_with_content(ui, "Receive File", "Rejecting File Transfer", "📥", |ui| {
ui.spinner();
});
}
ReceiveState::Completed(downloaded_path) => {
let downloaded_path = downloaded_path.clone();
Expand Down Expand Up @@ -211,7 +205,7 @@ enum ReceivePageResponse {
}

fn show_receive_file_page(ui: &mut Ui, code: &mut String) -> Option<ReceivePageResponse> {
crate::page_with_content(
page_with_content(
ui,
"Receive File",
"Enter the code from your peer below:",
Expand Down Expand Up @@ -249,7 +243,7 @@ fn show_connected_page(
ui: &mut Ui,
_receive_request: &ReceiveRequest,
) -> Option<ConnectedPageResponse> {
crate::page_with_content(ui, "Receive File", "TODO", "📥", |ui| {
page_with_content(ui, "Receive File", "TODO", "📥", |ui| {
if ui.button("Reject").clicked() {
return Some(ConnectedPageResponse::Reject);
}
Expand Down
16 changes: 8 additions & 8 deletions src/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::egui_ext::ContextExt;
use crate::error::PortalError;
use crate::sync::BorrowingOneshotReceiver;
use crate::transmit_info::transit_info_message;
use crate::widgets::{cancel_button, CancelLabel};
use crate::widgets::{cancel_button, page, page_with_content, CancelLabel};
use async_std::fs::File;
use eframe::{
egui::{Button, Context, Key, Modifiers, ProgressBar, Ui},
Expand Down Expand Up @@ -123,7 +123,7 @@ impl SendView {
}

fn show_file_selection_page(&mut self, ui: &mut Ui) {
crate::page_with_content(
page_with_content(
ui,
"Send File",
"Select or drop the file or directory to send.",
Expand Down Expand Up @@ -155,7 +155,7 @@ impl SendView {
}

fn show_transmit_code_progress(&self, ui: &mut Ui) {
crate::page_with_content(
page_with_content(
ui,
"Send File",
"Generating transmit code...",
Expand All @@ -167,7 +167,7 @@ impl SendView {
}

fn show_transmit_code(&self, ui: &mut Ui, welcome: &WormholeWelcome, file_path: &Path) {
crate::page_with_content(
page_with_content(
ui,
"Your Transmit Code",
format!(
Expand All @@ -188,13 +188,13 @@ impl SendView {

fn show_error_page(&mut self, ui: &mut Ui, error: String) {
self.back_button(ui);
crate::page(ui, "File Transfer Failed", error, "❌");
page(ui, "File Transfer Failed", error, "❌");
}

fn show_transfer_completed_page(&mut self, ui: &mut Ui, send_request: SendRequest) {
let filename = send_request.path().file_name().unwrap();
self.back_button(ui);
crate::page(
page(
ui,
"File Transfer Successful",
format!("Successfully sent file \"{}\"", filename.to_string_lossy()),
Expand Down Expand Up @@ -226,7 +226,7 @@ fn show_transfer_progress(
let Progress { sent, total } = controller.progress();
let filename = send_request.path().file_name().unwrap();
match controller.transit_info() {
Some(transit_info) => crate::page_with_content(
Some(transit_info) => page_with_content(
ui,
"Sending File",
transit_info_message(transit_info, filename),
Expand All @@ -235,7 +235,7 @@ fn show_transfer_progress(
ui.add(ProgressBar::new((sent as f64 / total as f64) as f32).animate(true));
},
),
None => crate::page_with_content(
None => page_with_content(
ui,
"Connected to Peer",
format!("Preparing to send file \"{}\"", filename.to_string_lossy()),
Expand Down
2 changes: 2 additions & 0 deletions src/widgets.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod cancel;
pub use self::cancel::*;
mod page;
pub use self::page::*;
mod tab_button;
pub use self::tab_button::*;
mod view_switcher;
Expand Down
28 changes: 28 additions & 0 deletions src/widgets/page.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use egui::{RichText, Ui, WidgetText};

pub fn page<'a>(
ui: &mut Ui,
title: impl Into<RichText>,
text: impl Into<WidgetText>,
icon: impl Into<Option<&'a str>>,
) {
if let Some(icon) = icon.into() {
ui.label(RichText::new(icon).size(120.0));
}
ui.add_space(10.0);
ui.label(title.into().size(30.0).strong());
ui.add_space(10.0);
ui.label(text);
}

pub fn page_with_content<'a, T>(
ui: &mut Ui,
title: impl Into<RichText>,
text: impl Into<WidgetText>,
icon: impl Into<Option<&'a str>>,
add_contents: impl FnOnce(&mut Ui) -> T,
) -> T {
page(ui, title, text, icon);
ui.add_space(20.0);
add_contents(ui)
}

0 comments on commit 328f21a

Please sign in to comment.