Skip to content

Commit

Permalink
Implement egui::Widget for custom widgets, use builder pattern for Popup
Browse files Browse the repository at this point in the history
  • Loading branch information
vv9k committed Dec 12, 2021
1 parent 1c290b4 commit 5f66639
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 153 deletions.
59 changes: 29 additions & 30 deletions src/app/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,12 @@ impl App {
});
egui::Grid::new("containers_button_menu").show(ui, |ui| {
if ui.button("prune").clicked() {
self.popups.push_back(ui::ActionPopup::new(
EventRequest::Container(ContainerEvent::Prune),
"Delete stopped containers",
"Are you sure you want to delete all stopped containers?",
));
self.popups.push_back(
ui::ActionPopup::builder(EventRequest::Container(ContainerEvent::Prune))
.title("Delete stopped containers")
.text("Are you sure you want to delete all stopped containers?")
.build(),
);
}
});
}
Expand Down Expand Up @@ -509,18 +510,21 @@ impl App {
.on_hover_text("Delete this container")
.clicked()
{
popup = Some(ui::ActionPopup::new(
EventRequest::Container(
ContainerEvent::Delete {
id: container.id.clone(),
},
),
"Delete container",
format!(
popup = Some(
ui::ActionPopup::builder(
EventRequest::Container(
ContainerEvent::Delete {
id: container.id.clone(),
},
),
)
.title("Delete container")
.text(format!(
"are you sure you want to delete container {}?",
&container.id
),
));
))
.build(),
);
}
match container.state {
ContainerStatus::Running => {
Expand Down Expand Up @@ -602,52 +606,47 @@ impl App {

ui.end_row();

let mut env = ui::EditableList::builder_key_val(&mut self.containers.create_data.env)
ui.add( ui::EditableList::builder_key_val(&mut self.containers.create_data.env)
.heading("Environment:")
.add_hover_text(
"Add a key value pair"
)
.key_heading("Key:")
.val_heading("Val:")
.build()
;
env.show(ui);
);
ui.end_row();

let mut sec_ops = ui::EditableList::builder_key(&mut self.containers.create_data.sec_ops)
ui.add(ui::EditableList::builder_key(&mut self.containers.create_data.sec_ops)
.heading("Security options:")
.add_hover_text(
"Add a security option"
).build()
;
sec_ops.show(ui);
);
ui.end_row();

let mut capabilities = ui::EditableList::builder_key(&mut self.containers.create_data.capabilities)
ui.add(ui::EditableList::builder_key(&mut self.containers.create_data.capabilities)
.heading("Capabilities:")
.add_hover_text(
"Add a capability"
).build()
;
capabilities.show(ui);
);
ui.end_row();

let mut volumes = ui::EditableList::builder_key(&mut self.containers.create_data.volumes)
ui.add(ui::EditableList::builder_key(&mut self.containers.create_data.volumes)
.heading("Volume mounts:")
.add_hover_text(
"Add a volume mount from host in the form of `/some/host/path:/some/container/path`)"
).build()
;
volumes.show(ui);
);
ui.end_row();

let mut links = ui::EditableList::builder_key(&mut self.containers.create_data.links)
ui.add(ui::EditableList::builder_key(&mut self.containers.create_data.links)
.heading("Links:")
.add_hover_text(
"Add a link"
).build()
;
links.show(ui);
);
ui.end_row();

key!(ui, "CPUs:");
Expand Down
37 changes: 20 additions & 17 deletions src/app/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,18 @@ impl App {
}
}
if ui.button("prune").clicked() {
self.popups.push_back(ui::ActionPopup::new(
EventRequest::Image(ImageEvent::Prune),
"Prune images",
self.popups.push_back(ui::ActionPopup::builder(
EventRequest::Image(ImageEvent::Prune)).title(
"Prune images").text(
"Are you sure you want to prune unused images? This will delete all images not in use by a container.",
));
).build());
}
if ui.button("clear cache").clicked() {
self.popups.push_back(ui::ActionPopup::new(
EventRequest::Image(ImageEvent::ClearCache),
"Clear images cache",
"Are you sure you want to clear image build cache?",
));
self.popups.push_back(ui::ActionPopup::builder(
EventRequest::Image(ImageEvent::ClearCache)).title(
"Clear images cache").text(
"Are you sure you want to clear image build cache?"
).build());
}
});
}
Expand Down Expand Up @@ -286,16 +286,19 @@ impl App {
.on_hover_text("delete the image")
.clicked()
{
popup = Some(ui::ActionPopup::new(
EventRequest::Image(ImageEvent::Delete {
id: image.id.clone(),
}),
"Delte image",
format!(
popup = Some(
ui::ActionPopup::builder(EventRequest::Image(
ImageEvent::Delete {
id: image.id.clone(),
},
))
.title("Delte image")
.text(format!(
"Are you sure you want to delete image {}",
&image.id
),
));
))
.build(),
);
}
if ui
.button(icon::SAVE)
Expand Down
24 changes: 13 additions & 11 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl App {
}
}

self.display_popups(ctx);
self.display_popups(ui);
});
}

Expand Down Expand Up @@ -291,9 +291,11 @@ impl App {
}
}

fn display_popups(&mut self, ctx: &egui::CtxRef) {
fn display_popups(&mut self, ui: &mut egui::Ui) {
use egui::Widget;
for popup in &mut self.popups {
popup.display(ctx);
popup.ui(ui);
popup.ui(ui);
}
}
}
Expand Down Expand Up @@ -509,11 +511,11 @@ impl App {
Err((id, e)) => match e {
docker_api::Error::Fault { code, message } => {
if code.as_u16() == 409 {
self.popups.push_back(ui::ActionPopup::new(
EventRequest::Container(ContainerEvent::ForceDelete { id }),
"Force delete container",
self.popups.push_back(ui::ActionPopup::builder(
EventRequest::Container(ContainerEvent::ForceDelete { id })).title(
"Force delete container").text(
format!("{}\nAre you sure you want to forcefully delete this container?", message),
));
).build());
} else {
self.add_error(format!(
"cannot force delete container {}: {}",
Expand Down Expand Up @@ -627,11 +629,11 @@ impl App {
Err((id, e)) => match e {
docker_api::Error::Fault { code, message } => {
if code.as_u16() == 409 && !message.contains("cannot be forced") {
self.popups.push_back(ui::ActionPopup::new(
EventRequest::Image(ImageEvent::ForceDelete { id }),
"Force delete image",
self.popups.push_back(ui::ActionPopup::builder(
EventRequest::Image(ImageEvent::ForceDelete { id })).title(
"Force delete image").text(
format!("{}\n Are you sure you want to forcefully delete this image?", message),
));
).build());
} else {
self.add_error(format!(
"cannot force delete image {}: {}",
Expand Down
43 changes: 22 additions & 21 deletions src/app/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ impl App {
});
egui::Grid::new("networks_button_grid").show(ui, |ui| {
if ui.button("prune").clicked() {
self.popups.push_back(ui::ActionPopup::new(
EventRequest::Network(NetworkEvent::Prune),
"Prune networks",
self.popups.push_back(ui::ActionPopup::builder(
EventRequest::Network(NetworkEvent::Prune)).title(
"Prune networks").text(
"Are you sure you want to prune unused networks? This will delete all networks not in use by a container.",
));
).build());
}
});
}
Expand Down Expand Up @@ -188,16 +188,16 @@ impl App {
.on_hover_text("delete the network")
.clicked()
{
popup = Some(ui::ActionPopup::new(
popup = Some(ui::ActionPopup::builder(
EventRequest::Network(NetworkEvent::Delete {
id: network.id.clone(),
}),
"Delete network",
})).title(
"Delete network").text(
format!(
"Are you sure you want to delete network {}",
&network.id
),
));
).build());
}
});
ui.end_row();
Expand Down Expand Up @@ -358,29 +358,29 @@ impl App {
ui.text_edit_singleline(&mut self.networks.create_view_data.driver);
ui.end_row();

let mut labels =
ui.add(
ui::EditableList::builder_key_val(&mut self.networks.create_view_data.labels)
.heading("Labels:")
.build();
labels.show(ui);
.build(),
);
ui.end_row();

let mut opts =
ui.add(
ui::EditableList::builder_key_val(&mut self.networks.create_view_data.opts)
.heading("Options:")
.build();
opts.show(ui);
.build(),
);
ui.end_row();
ui.end_row();

key!(ui, "IPAM Driver:");
ui.text_edit_singleline(&mut self.networks.create_view_data.ipam_driver);
ui.end_row();
let mut ipam_opts =
ui.add(
ui::EditableList::builder_key_val(&mut self.networks.create_view_data.ipam_opts)
.heading("IPAM Options:")
.build();
ipam_opts.show(ui);
.build(),
);
ui.end_row();
key!(ui, "IPAM Config:");
if ui.button(icon::ADD).clicked() {
Expand Down Expand Up @@ -408,10 +408,11 @@ impl App {
key!(ui, &name);
});
ui.end_row();
let mut cfg = ui::EditableList::builder_key_val(config)
.heading(&name)
.build();
cfg.show(ui);
ui.add(
ui::EditableList::builder_key_val(config)
.heading(&name)
.build(),
);
ui.end_row();
}
});
Expand Down
4 changes: 3 additions & 1 deletion src/app/ui/editable_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ impl<'a> EditableList<'a> {
add_hover_text: None,
}
}
}

pub fn show(&mut self, ui: &mut egui::Ui) -> egui::Response {
impl<'a> egui::Widget for EditableList<'a> {
fn ui(mut self, ui: &mut egui::Ui) -> egui::Response {
if let Some(heading) = self.heading {
key!(ui, heading);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use egui::{
use epaint::Shadow;

pub use editable_list::{EditableList, EditableListBuilder};
pub use popup::{popup, ActionPopup, Popup};
pub use popup::{ActionPopup, Popup};

pub mod color {
use egui::{Color32, Rgba};
Expand Down
Loading

0 comments on commit 5f66639

Please sign in to comment.