Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for window resizing #353

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use std::cell::{Cell, LazyCell};
use std::future::Future;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::atomic::{AtomicI32, Ordering};
use std::{env, fmt};
use tracing::{debug, error, warn};

Expand Down Expand Up @@ -116,6 +117,7 @@ pub enum AppInput {
StartUpgrade,
Restart,
CloseStatusBarWarning,
WindowResized,
HideWindow,
ShowWindow,
ShowHideToggle,
Expand Down Expand Up @@ -281,7 +283,6 @@ impl AsyncComponent for App {
view! {
gtk::Window {
set_decorated: false,
set_resizable: false,
set_size_request: (800, 600),
#[track = "model.changed_current_view()"]
set_title: Some(&format!("{} - Space Acres {}", model.current_view.title(), env!("CARGO_PKG_VERSION"))),
Expand Down Expand Up @@ -721,6 +722,22 @@ impl AsyncComponent for App {
});
}

// Hacky way to track window size changes
if let Some(surface) = root.surface() {
surface.connect_notify(Some("width"), {
let sender = sender.clone();
let last_width = AtomicI32::new(0);

move |surface, _new_state_param| {
let new_surface_width = surface.width();
let old_surface_width = last_width.swap(new_surface_width, Ordering::AcqRel);
if new_surface_width != old_surface_width {
sender.input(AppInput::WindowResized);
}
}
});
}

AsyncComponentParts { model, widgets }
}

Expand Down Expand Up @@ -794,6 +811,9 @@ impl AsyncComponent for App {
AppInput::CloseStatusBarWarning => {
self.set_status_bar_contents(StatusBarContents::None);
}
AppInput::WindowResized => {
self.running_view.emit(RunningInput::WindowResized);
}
AppInput::HideWindow => {
root.set_visible(false);
}
Expand Down
4 changes: 4 additions & 0 deletions src/frontend/running.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum RunningInput {
FarmerNotification(FarmerNotification<FarmIndex>),
ToggleFarmDetails,
TogglePausePlotting,
WindowResized,
}

#[derive(Debug)]
Expand Down Expand Up @@ -493,6 +494,9 @@ impl RunningView {
debug!("Failed to send RunningOutput::TogglePausePlotting");
}
}
RunningInput::WindowResized => {
self.farms.broadcast(FarmWidgetInput::WindowResized);
}
}
}

Expand Down
63 changes: 50 additions & 13 deletions src/frontend/running/farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bytesize::ByteSize;
use gtk::prelude::*;
use notify_rust::Notification;
use relm4::prelude::*;
use relm4::{RelmIterChildrenExt, RelmRemoveAllExt};
use simple_moving_average::{SingleSumSMA, SMA};
use std::collections::HashMap;
use std::path::PathBuf;
Expand All @@ -19,8 +20,6 @@ use subspace_farmer::farm::{
use tracing::{error, warn};

const INVALID_SCORE_VALUE: f64 = -1.0;
/// Experimentally found number that is good for default window size to not have horizontal scroll
const SECTORS_PER_ROW: usize = 108;
/// Number of samples over which to track auditing time, 1 minute in slots
const AUDITING_TIME_TRACKING_WINDOW: usize = 60;
/// 500ms auditing time is excellent, anything larger will result in auditing performance indicator decrease
Expand Down Expand Up @@ -99,6 +98,8 @@ pub(super) enum FarmWidgetInput {
Error {
error: Arc<anyhow::Error>,
},
WindowResized,
RecalculateSectorRows,
}

#[tracker::track]
Expand Down Expand Up @@ -380,11 +381,14 @@ impl FactoryComponent for FarmWidget {
},
},

gtk::Box {
#[track = "self.changed_farm_details() || self.changed_error()"]
set_visible: self.farm_details && self.error.is_none(),
// A hack to allow measuring width for sectors
gtk::ScrolledWindow {
set_vscrollbar_policy: gtk::PolicyType::Never,

self.sector_rows.clone(),
self.sector_rows.clone() -> gtk::Box {
#[track = "self.changed_farm_details() || self.changed_error()"]
set_visible: self.farm_details && self.error.is_none(),
},
},
},
}
Expand All @@ -394,6 +398,7 @@ impl FactoryComponent for FarmWidget {
for sector_index in 0..init.total_sectors {
let sector = gtk::Box::builder()
.css_name("farm-sector")
.halign(gtk::Align::Start)
.tooltip_text(T.running_farmer_farm_sector(sector_index).as_str())
.build();
if sector_index < init.plotted_total_sectors {
Expand All @@ -404,13 +409,6 @@ impl FactoryComponent for FarmWidget {
}

let sector_rows = gtk::Box::new(gtk::Orientation::Vertical, 0);
sectors.chunks(SECTORS_PER_ROW).for_each(|sectors| {
let sector_row = gtk::Box::new(gtk::Orientation::Horizontal, 0);
sector_rows.append(&sector_row);
for sector in sectors {
sector_row.append(sector);
}
});

Self {
path: init.farm.directory,
Expand Down Expand Up @@ -585,6 +583,7 @@ impl FarmWidget {
}
FarmWidgetInput::ToggleFarmDetails => {
self.set_farm_details(!self.farm_details);
sender.input(FarmWidgetInput::RecalculateSectorRows);
}
FarmWidgetInput::Error { error } => {
sender.spawn_command(|_sender| {
Expand All @@ -602,6 +601,44 @@ impl FarmWidget {

self.get_mut_error().replace(error);
}
FarmWidgetInput::WindowResized => {
sender.input(FarmWidgetInput::RecalculateSectorRows);
}
FarmWidgetInput::RecalculateSectorRows => {
if !self.farm_details {
return;
}

let sector_rows_width = self
.sector_rows
.parent()
.expect("Always attached to parent; qed")
.width();
// TODO: This is a hardcoded number, but it is tricky to determine its size in
// runtime, so will do for now
let sector_width = 7;
let sectors_per_row = sector_rows_width / sector_width;

if sectors_per_row as usize == self.sector_rows.iter_children().count() {
// Nothing has changed
return;
}

let mut sectors_iter = self.sectors.values();

self.sector_rows.remove_all();
for _ in 0..self.sectors.len().div_ceil(sectors_per_row as usize) {
let sector_row = gtk::Box::new(gtk::Orientation::Horizontal, 0);

for _ in 0..sectors_per_row {
if let Some(sector) = sectors_iter.next() {
sector_row.append(sector);
}
}

self.sector_rows.append(&sector_row);
}
}
}
}

Expand Down