Skip to content

Commit c926113

Browse files
jieyouxutrumank
authored andcommitted
Allow multiple mod detail windows
1 parent fc933a7 commit c926113

File tree

2 files changed

+60
-45
lines changed

2 files changed

+60
-45
lines changed

src/gui/message.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,7 @@ async fn self_update_async(
754754
#[derive(Debug)]
755755
pub struct FetchModDetails {
756756
rid: RequestID,
757+
modio_id: u32,
757758
result: Result<ModDetails>,
758759
}
759760

@@ -780,34 +781,44 @@ impl FetchModDetails {
780781
rid,
781782
handle: tokio::task::spawn(async move {
782783
let result = fetch_modio_mod_details(oauth_token, modio_id).await;
783-
tx.send(Message::FetchModDetails(FetchModDetails { rid, result }))
784-
.await
785-
.unwrap();
784+
tx.send(Message::FetchModDetails(FetchModDetails {
785+
rid,
786+
result,
787+
modio_id,
788+
}))
789+
.await
790+
.unwrap();
786791
ctx.request_repaint();
787792
}),
788793
state: (),
789794
}
790795
}
791796

792797
fn receive(self, app: &mut App) {
793-
if Some(self.rid) == app.fetch_mod_details_rid.as_ref().map(|r| r.rid) {
798+
let mut to_remove = None;
799+
800+
if let Some(req) = app.fetch_mod_details_rid.get(&self.modio_id)
801+
&& req.rid == self.rid
802+
{
794803
match self.result {
795804
Ok(mod_details) => {
796805
info!("fetch mod details successful");
797-
app.mod_details = Some(mod_details);
806+
app.mod_details.insert(mod_details.r#mod.id, mod_details);
798807
app.last_action_status =
799808
LastActionStatus::Success("fetch mod details complete".to_string());
800809
}
801810
Err(e) => {
802811
error!("fetch mod details failed");
803812
error!("{:#?}", e);
804-
app.mod_details = None;
805-
app.fetch_mod_details_rid = None;
813+
to_remove = Some(self.modio_id);
806814
app.last_action_status =
807815
LastActionStatus::Failure("fetch mod details failed".to_string());
808816
}
809817
}
810-
app.integrate_rid = None;
818+
}
819+
820+
if let Some(id) = to_remove {
821+
app.fetch_mod_details_rid.remove(&id);
811822
}
812823
}
813824
}

src/gui/mod.rs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ pub struct App {
103103
needs_restart: bool,
104104
self_update_rid: Option<MessageHandle<SelfUpdateProgress>>,
105105
original_exe_path: Option<PathBuf>,
106-
detailed_mod_info_window: Option<WindowDetailedModInfo>,
107-
mod_details: Option<ModDetails>,
108-
fetch_mod_details_rid: Option<MessageHandle<()>>,
109-
mod_details_thumbnail_texture_handle: Option<egui::TextureHandle>,
106+
detailed_mod_info_windows: HashMap<u32, WindowDetailedModInfo>,
107+
mod_details: HashMap<u32, ModDetails>,
108+
fetch_mod_details_rid: HashMap<u32, MessageHandle<()>>,
109+
mod_details_thumbnail_texture_handle: HashMap<u32, egui::TextureHandle>,
110110
}
111111

112112
#[derive(Default)]
@@ -166,10 +166,10 @@ impl App {
166166
needs_restart: false,
167167
self_update_rid: None,
168168
original_exe_path: None,
169-
detailed_mod_info_window: None,
170-
mod_details: None,
171-
fetch_mod_details_rid: None,
172-
mod_details_thumbnail_texture_handle: None,
169+
detailed_mod_info_windows: HashMap::default(),
170+
mod_details: HashMap::default(),
171+
fetch_mod_details_rid: HashMap::default(),
172+
mod_details_thumbnail_texture_handle: HashMap::default(),
173173
})
174174
}
175175

@@ -451,9 +451,8 @@ impl App {
451451
.on_hover_text_at_pointer("View details")
452452
.clicked()
453453
{
454-
self.detailed_mod_info_window =
455-
Some(WindowDetailedModInfo { info: info.clone() });
456-
self.fetch_mod_details_rid = Some(message::FetchModDetails::send(
454+
self.detailed_mod_info_windows.insert(modio_id, WindowDetailedModInfo { info: info.clone() });
455+
self.fetch_mod_details_rid.insert(modio_id, message::FetchModDetails::send(
457456
&mut self.request_counter,
458457
ui.ctx(),
459458
self.tx.clone(),
@@ -1429,40 +1428,35 @@ impl App {
14291428
}
14301429
}
14311430

1432-
fn show_detailed_mod_info(&mut self, ctx: &egui::Context) {
1433-
if let Some(WindowDetailedModInfo { info }) = &self.detailed_mod_info_window {
1434-
egui::Area::new("detailed-mod-info-overlay")
1435-
.movable(false)
1436-
.fixed_pos(Pos2::ZERO)
1437-
.order(egui::Order::Background)
1438-
.show(ctx, |ui| {
1439-
egui::Frame::none()
1440-
.fill(Color32::from_rgba_unmultiplied(0, 0, 0, 127))
1441-
.show(ui, |ui| {
1442-
ui.allocate_space(ui.available_size());
1443-
})
1444-
});
1431+
fn show_detailed_mod_info(&mut self, ctx: &egui::Context, modio_id: u32) {
1432+
let mut to_remove = Vec::new();
14451433

1434+
if let Some(WindowDetailedModInfo { info }) = self.detailed_mod_info_windows.get(&modio_id)
1435+
{
14461436
let mut open = true;
14471437

14481438
egui::Window::new(&info.name)
14491439
.open(&mut open)
14501440
.collapsible(false)
1451-
.anchor(Align2::CENTER_TOP, Vec2::new(0.0, 30.0))
1452-
.resizable(false)
1453-
.show(ctx, |ui| self.show_detailed_mod_info_inner(ui));
1441+
.movable(true)
1442+
.resizable(true)
1443+
.show(ctx, |ui| self.show_detailed_mod_info_inner(ui, modio_id));
14541444

14551445
if !open {
1456-
self.detailed_mod_info_window = None;
1457-
self.mod_details = None;
1458-
self.fetch_mod_details_rid = None;
1459-
self.mod_details_thumbnail_texture_handle = None;
1446+
to_remove.push(modio_id);
14601447
}
14611448
}
1449+
1450+
for id in to_remove {
1451+
self.detailed_mod_info_windows.remove(&id);
1452+
self.mod_details.remove(&id);
1453+
self.fetch_mod_details_rid.remove(&id);
1454+
self.mod_details_thumbnail_texture_handle.remove(&id);
1455+
}
14621456
}
14631457

1464-
fn show_detailed_mod_info_inner(&mut self, ui: &mut egui::Ui) {
1465-
if let Some(mod_details) = &self.mod_details {
1458+
fn show_detailed_mod_info_inner(&mut self, ui: &mut egui::Ui, modio_id: u32) {
1459+
if let Some(mod_details) = &self.mod_details.get(&modio_id) {
14661460
let scroll_area_height = (ui.available_height() - 60.0).clamp(0.0, f32::INFINITY);
14671461

14681462
egui::ScrollArea::vertical()
@@ -1473,7 +1467,8 @@ impl App {
14731467
.show(ui, |ui| {
14741468
let texture: &egui::TextureHandle = self
14751469
.mod_details_thumbnail_texture_handle
1476-
.get_or_insert_with(|| {
1470+
.entry(modio_id)
1471+
.or_insert_with(|| {
14771472
ui.ctx().load_texture(
14781473
format!("{} image", mod_details.r#mod.name),
14791474
{
@@ -1646,7 +1641,16 @@ impl eframe::App for App {
16461641
self.show_settings(ctx);
16471642
self.show_lints_toggle(ctx);
16481643
self.show_lint_report(ctx);
1649-
self.show_detailed_mod_info(ctx);
1644+
1645+
let modio_ids = self
1646+
.detailed_mod_info_windows
1647+
.keys()
1648+
.copied()
1649+
.collect::<Vec<_>>();
1650+
1651+
for modio_id in modio_ids {
1652+
self.show_detailed_mod_info(ctx, modio_id);
1653+
}
16501654

16511655
egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
16521656
ui.with_layout(egui::Layout::right_to_left(Align::TOP), |ui| {
@@ -1655,8 +1659,8 @@ impl eframe::App for App {
16551659
&& self.update_rid.is_none()
16561660
&& self.lint_rid.is_none()
16571661
&& self.self_update_rid.is_none()
1658-
&& self.detailed_mod_info_window.is_none()
1659-
&& self.fetch_mod_details_rid.is_none()
1662+
&& self.detailed_mod_info_windows.is_empty()
1663+
&& self.fetch_mod_details_rid.is_empty()
16601664
&& self.state.config.drg_pak_path.is_some(),
16611665
|ui| {
16621666
if let Some(args) = &self.args {

0 commit comments

Comments
 (0)