diff --git a/Cargo.lock b/Cargo.lock index 71ccbc6..46b1e4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -297,6 +297,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "block2" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdeb9d870516001442e364c5220d3574d2da8dc765554b4a617230d33fa58ef5" +dependencies = [ + "objc2", +] + [[package]] name = "bon" version = "3.9.1" @@ -789,6 +798,12 @@ dependencies = [ "cmov", ] +[[package]] +name = "cursor-icon" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f27ae1dd37df86211c42e150270f82743308803d90a6f6e6651cd730d5e1732f" + [[package]] name = "darling" version = "0.21.3" @@ -1019,6 +1034,37 @@ dependencies = [ "winapi", ] +[[package]] +name = "dispatch2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0e367e4e7da84520dedcac1901e4da967309406d1e51017ae1abfb97adbd38" +dependencies = [ + "bitflags 2.13.1", + "objc2", +] + +[[package]] +name = "display-info" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e0aca670967c2528799e316f9f97913efcc034867614d55681dd41a1c2f7830" +dependencies = [ + "fxhash", + "log", + "objc2", + "objc2-app-kit", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-foundation", + "scopeguard", + "smithay-client-toolkit", + "thiserror 2.0.18", + "widestring", + "windows 0.62.2", + "xcb", +] + [[package]] name = "displaydoc" version = "0.2.5" @@ -1048,6 +1094,12 @@ dependencies = [ "litrs", ] +[[package]] +name = "downcast-rs" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" + [[package]] name = "either" version = "1.15.0" @@ -1392,6 +1444,15 @@ dependencies = [ "slab", ] +[[package]] +name = "fxhash" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c" +dependencies = [ + "byteorder", +] + [[package]] name = "gdb-command" version = "0.7.8" @@ -2623,6 +2684,174 @@ dependencies = [ "url", ] +[[package]] +name = "objc2" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a12a8ed07aefc768292f076dc3ac8c48f3781c8f2d5851dd3d98950e8c5a89f" +dependencies = [ + "objc2-encode", +] + +[[package]] +name = "objc2-app-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d49e936b501e5c5bf01fda3a9452ff86dc3ea98ad5f283e1455153142d97518c" +dependencies = [ + "bitflags 2.13.1", + "block2", + "libc", + "objc2", + "objc2-cloud-kit", + "objc2-core-data", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-core-image", + "objc2-core-text", + "objc2-core-video", + "objc2-foundation", + "objc2-quartz-core", +] + +[[package]] +name = "objc2-cloud-kit" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73ad74d880bb43877038da939b7427bba67e9dd42004a18b809ba7d87cee241c" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-core-data" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b402a653efbb5e82ce4df10683b6b28027616a2715e90009947d50b8dd298fa" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-core-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536" +dependencies = [ + "bitflags 2.13.1", + "block2", + "dispatch2", + "libc", + "objc2", +] + +[[package]] +name = "objc2-core-graphics" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807" +dependencies = [ + "bitflags 2.13.1", + "block2", + "dispatch2", + "libc", + "objc2", + "objc2-core-foundation", + "objc2-io-surface", + "objc2-metal", +] + +[[package]] +name = "objc2-core-image" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d563b38d2b97209f8e861173de434bd0214cf020e3423a52624cd1d989f006" +dependencies = [ + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-core-text" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0cde0dfb48d25d2b4862161a4d5fcc0e3c24367869ad306b0c9ec0073bfed92d" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-core-foundation", + "objc2-core-graphics", +] + +[[package]] +name = "objc2-core-video" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d425caf1df73233f29fd8a5c3e5edbc30d2d4307870f802d18f00d83dc5141a6" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-core-foundation", + "objc2-core-graphics", + "objc2-io-surface", +] + +[[package]] +name = "objc2-encode" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef25abbcd74fb2609453eb695bd2f860d389e457f67dc17cafc8b8cbc89d0c33" + +[[package]] +name = "objc2-foundation" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3e0adef53c21f888deb4fa59fc59f7eb17404926ee8a6f59f5df0fd7f9f3272" +dependencies = [ + "bitflags 2.13.1", + "block2", + "libc", + "objc2", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-io-surface" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180788110936d59bab6bd83b6060ffdfffb3b922ba1396b312ae795e1de9d81d" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-core-foundation", +] + +[[package]] +name = "objc2-metal" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-foundation", +] + +[[package]] +name = "objc2-quartz-core" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c1358452b371bf9f104e21ec536d37a650eb10f7ee379fff67d2e08d537f1f" +dependencies = [ + "bitflags 2.13.1", + "objc2", + "objc2-foundation", +] + [[package]] name = "object" version = "0.37.3" @@ -3150,6 +3379,15 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" +[[package]] +name = "quick-xml" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e660451e55124f798a69a5af3f49ccfbefbd41910eefd25caf2393e1f3473ec1" +dependencies = [ + "memchr", +] + [[package]] name = "quinn" version = "0.11.9" @@ -3366,7 +3604,7 @@ dependencies = [ "rustix 0.38.44", "self_cell", "thiserror 1.0.69", - "windows", + "windows 0.58.0", ] [[package]] @@ -3653,6 +3891,7 @@ dependencies = [ "crossterm 0.29.0", "dirs", "dirs-next", + "display-info", "fast-strip-ansi", "fastnbt", "flate2", @@ -4170,6 +4409,31 @@ version = "1.15.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" +[[package]] +name = "smithay-client-toolkit" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0512da38f5e2b31201a93524adb8d3136276fa4fe4aafab4e1f727a82b534cc0" +dependencies = [ + "bitflags 2.13.1", + "cursor-icon", + "libc", + "log", + "memmap2", + "rustix 1.1.4", + "thiserror 2.0.18", + "wayland-backend", + "wayland-client", + "wayland-csd-frame", + "wayland-cursor", + "wayland-protocols", + "wayland-protocols-experimental", + "wayland-protocols-misc", + "wayland-protocols-wlr", + "wayland-scanner", + "xkeysym", +] + [[package]] name = "socket2" version = "0.6.3" @@ -5294,6 +5558,124 @@ dependencies = [ "semver", ] +[[package]] +name = "wayland-backend" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38a91b4eaddff87b1cd1074985e3713da4af2c49742d1b356b2c01670a67a078" +dependencies = [ + "cc", + "downcast-rs", + "rustix 1.1.4", + "smallvec", + "wayland-sys", +] + +[[package]] +name = "wayland-client" +version = "0.31.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3c36a0f861ad76d0901f2800b46321410d9f73f2ea88aac0650d86c32688073" +dependencies = [ + "bitflags 2.13.1", + "rustix 1.1.4", + "wayland-backend", + "wayland-scanner", +] + +[[package]] +name = "wayland-csd-frame" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "625c5029dbd43d25e6aa9615e88b829a5cad13b2819c4ae129fdbb7c31ab4c7e" +dependencies = [ + "bitflags 2.13.1", + "cursor-icon", + "wayland-backend", +] + +[[package]] +name = "wayland-cursor" +version = "0.31.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a52d18780be9b1314328a3de5f930b73d2200112e3849ca6cb11822793fb34d" +dependencies = [ + "rustix 1.1.4", + "wayland-client", + "xcursor", +] + +[[package]] +name = "wayland-protocols" +version = "0.32.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d0c813de3daa2ed6520af85a3bd49b0e722a3078506899aa9686fea58dc4b6" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-experimental" +version = "20250721.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40a1f863128dcaaec790d7b4b396cc9b9a7a079e878e18c47e6c2d2c5a8dcbb1" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-misc" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e9567599ef23e09b8dad6e429e5738d4509dfc46b3b21f32841a304d16b29c8" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-protocols-wlr" +version = "0.3.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb04e52f7836d7c7976c78ca0250d61e33873c34156a2a1fc9474828ec268234" +dependencies = [ + "bitflags 2.13.1", + "wayland-backend", + "wayland-client", + "wayland-protocols", + "wayland-scanner", +] + +[[package]] +name = "wayland-scanner" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "338e30461b3a2b67d70eb30a6d89f8e0c93a833e07d2ae89085cd070c4a00ac0" +dependencies = [ + "proc-macro2", + "quick-xml", + "quote", +] + +[[package]] +name = "wayland-sys" +version = "0.31.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8eab23fefc9e41f8e841df4a9c707e8a8c4ed26e944ef69297184de2785e3be" +dependencies = [ + "pkg-config", +] + [[package]] name = "web-sys" version = "0.3.95" @@ -5434,6 +5816,12 @@ dependencies = [ "safe_arch", ] +[[package]] +name = "widestring" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" + [[package]] name = "winapi" version = "0.3.9" @@ -5475,6 +5863,27 @@ dependencies = [ "windows-targets 0.52.6", ] +[[package]] +name = "windows" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" +dependencies = [ + "windows-collections", + "windows-core 0.62.2", + "windows-future", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" +dependencies = [ + "windows-core 0.62.2", +] + [[package]] name = "windows-core" version = "0.58.0" @@ -5501,6 +5910,17 @@ dependencies = [ "windows-strings 0.5.1", ] +[[package]] +name = "windows-future" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" +dependencies = [ + "windows-core 0.62.2", + "windows-link", + "windows-threading", +] + [[package]] name = "windows-implement" version = "0.58.0" @@ -5551,6 +5971,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-numerics" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" +dependencies = [ + "windows-core 0.62.2", + "windows-link", +] + [[package]] name = "windows-result" version = "0.2.0" @@ -5657,6 +6087,15 @@ dependencies = [ "windows_x86_64_msvc 0.53.1", ] +[[package]] +name = "windows-threading" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" +dependencies = [ + "windows-link", +] + [[package]] name = "windows_aarch64_gnullvm" version = "0.52.6" @@ -5894,6 +6333,29 @@ dependencies = [ "tap", ] +[[package]] +name = "xcb" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6c2ad15e0e922856ee89afe862b8992334bbe7953adad56cd1199358cb30566" +dependencies = [ + "bitflags 2.13.1", + "libc", + "quick-xml", +] + +[[package]] +name = "xcursor" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "163b33ed8786455e2fa5d72f554057ce3f3182425434f756cd39c99839d88e23" + +[[package]] +name = "xkeysym" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9cc00251562a284751c9973bace760d86c0276c471b4be569fe6b068ee97a56" + [[package]] name = "xmlwriter" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index bd8d2de..e828a24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ clap = "4.5.27" color-eyre = "0.6.3" config = "0.15.6" crossterm = "0.29.0" +display-info = "0.5.9" dirs-next = "2.0.0" log = "0.4" ratatui = { version = "0.30.0", features = ["serde", "unstable-rendered-line-info"] } diff --git a/src/tui/tests/flows.rs b/src/tui/tests/flows.rs index e3cfdc9..c0c0eee 100644 --- a/src/tui/tests/flows.rs +++ b/src/tui/tests/flows.rs @@ -585,7 +585,8 @@ fn settings_panel_routes_legacy_edit_keys_to_tui_popups() { assert!(ui.screen().contains("Game version")); assert!(ui.screen().contains("Memory min")); assert!(ui.screen().contains("Desktop")); - assert!(ui.screen().contains('█')); + assert!(ui.screen().contains('◆')); + assert!(!ui.screen().contains('█')); assert!(!ui.screen().contains("● enabled")); assert!(!ui.screen().contains("Integration")); assert!(!ui.screen().contains('▰')); @@ -617,7 +618,8 @@ fn settings_panel_routes_legacy_edit_keys_to_tui_popups() { ui.draw(); assert!(ui.screen().contains("Launcher Settings")); assert!(ui.screen().contains("Memory max")); - assert!(ui.screen().contains('█')); + assert!(ui.screen().contains('◆')); + assert!(!ui.screen().contains('█')); assert!(!ui.screen().contains('▰')); ui.key(KeyCode::Enter); ui.draw(); @@ -699,6 +701,10 @@ fn settings_use_java_memory_and_resolution_controls() { ui.draw(); assert!(ui.screen().contains("Resolution")); assert!(ui.screen().contains("1920x1080")); + for _ in 0..20 { + ui.key(KeyCode::Char('j')); + } + ui.draw(); assert!(ui.screen().contains("Custom")); ui.key(KeyCode::Esc); ui.key(KeyCode::Esc); diff --git a/src/tui/widgets/popups/global_settings.rs b/src/tui/widgets/popups/global_settings.rs index 743652e..bbe15b4 100644 --- a/src/tui/widgets/popups/global_settings.rs +++ b/src/tui/widgets/popups/global_settings.rs @@ -292,13 +292,12 @@ impl State { KeyCode::Enter => match self.selected { 0 => self.theme_picker = true, 1 => self.cycle_border(true), - 2 | 3 => self.adjust_selected_memory(true), + 2 | 3 => { + self.editing = Some(new_text_area(vec![self.value(self.selected)])); + } 4 => self.open_java_picker(), field => self.editing = Some(new_text_area(vec![self.value(field)])), }, - KeyCode::Char('c') if matches!(self.selected, 2 | 3) => { - self.editing = Some(new_text_area(vec![self.value(self.selected)])); - } KeyCode::Char('s') => { if self.validate_before_save() { return Action::Save( @@ -380,8 +379,9 @@ pub fn popup_rect(area: Rect, state: &State) -> Rect { } else { 7 + u16::from(state.error.is_some()) }; + let width = if state.java_picker_open { 72 } else { 52 }; area.centered( - ratatui::layout::Constraint::Percentage(52), + ratatui::layout::Constraint::Percentage(width), ratatui::layout::Constraint::Length(height.min(area.height.saturating_sub(4))), ) } @@ -399,7 +399,7 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { } else if matches!(state.selected, 2 | 3) { super::keybind_line(&[ ("h/l", " adjust"), - ("c", " custom"), + ("Enter", " exact"), ("s", " save"), ("Esc", " back"), ]) @@ -470,11 +470,11 @@ fn render_java_picker(frame: &mut Frame, area: Rect, state: &State) { list_area.y = list_area.y.saturating_add(1); list_area.height = list_area.height.saturating_sub(1); } - render_picker( - frame, - list_area, - &state.java_picker.labels(), + super::select_list::render( + state.java_picker.items(), state.java_picker.selected, + list_area, + frame.buffer_mut(), ); } @@ -553,6 +553,11 @@ fn global_field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a }), ), ]) + .style(Style::default().bg(if selected { + theme.stripe() + } else { + theme.surface() + })) } #[cfg(test)] @@ -564,10 +569,14 @@ mod tests { let mut state = State::new(); state.selected = 2; let original = state.config.defaults.memory_min.clone(); - state.handle_key(&KeyEvent::from(KeyCode::Enter)); + state.handle_key(&KeyEvent::from(KeyCode::Char('l'))); assert_ne!(state.config.defaults.memory_min, original); assert!(state.editing.is_none()); + state.handle_key(&KeyEvent::from(KeyCode::Enter)); + assert!(state.editing.is_some()); + + state.editing = None; state.selected = 4; state.handle_key(&KeyEvent::from(KeyCode::Enter)); assert!(state.java_picker_open); diff --git a/src/tui/widgets/popups/instance_settings.rs b/src/tui/widgets/popups/instance_settings.rs index ba8ba15..056d3c9 100644 --- a/src/tui/widgets/popups/instance_settings.rs +++ b/src/tui/widgets/popups/instance_settings.rs @@ -25,7 +25,8 @@ use crate::{ popups::{ LoadState, settings_controls::{ - JavaChoice, JavaPicker, adjust_memory, memory_kib, render_memory_gauge, + DisplayResolution, JavaChoice, JavaPicker, adjust_memory, badge, + display_resolutions, memory_kib, render_memory_gauge, }, }, search::SearchState, @@ -48,6 +49,38 @@ enum ChoicePicker { Resolution, } +#[derive(Debug, Clone, PartialEq, Eq)] +enum ResolutionChoice { + Default, + Display(DisplayResolution), + Preset(u32, u32), + Configured(u32, u32), + Custom, +} + +impl ResolutionChoice { + fn resolution(&self) -> Option<(u32, u32)> { + match self { + Self::Display(display) => Some((display.width, display.height)), + Self::Preset(width, height) | Self::Configured(width, height) => { + Some((*width, *height)) + } + Self::Default | Self::Custom => None, + } + } + + fn label(&self) -> String { + self.resolution().map_or_else( + || match self { + Self::Default => "Default".to_owned(), + Self::Custom => "Custom…".to_owned(), + _ => String::new(), + }, + |(width, height)| format!("{width}x{height}"), + ) + } +} + enum PickerLoad { Idle, Loading, @@ -74,6 +107,7 @@ pub struct State { choice_picker: Option, choice_index: usize, java_picker: JavaPicker, + display_resolutions: Vec, } pub enum Action { @@ -109,6 +143,7 @@ impl State { choice_picker: None, choice_index: 0, java_picker: JavaPicker::new(), + display_resolutions: display_resolutions(), } } @@ -202,7 +237,9 @@ impl State { } 2 => self.open_loader_picker(), 3 => self.open_choice_picker(ChoicePicker::Java), - 4 | 5 => self.adjust_selected_memory(true), + 4 | 5 => { + self.editing = Some(new_text_area(vec![self.effective_memory(self.selected)])); + } 6 => self.editing = Some(new_text_area(vec![self.value(self.selected)])), 7 => self.open_choice_picker(ChoicePicker::Resolution), 8 => self.desktop = !self.desktop, @@ -222,14 +259,10 @@ impl State { self.java_picker.initialize(); self.java_picker.selected } - ChoicePicker::Resolution => resolution_values(self.draft.resolution) + ChoicePicker::Resolution => self + .resolution_choices() .iter() - .position(|value| { - self.draft.resolution.map_or_else( - || value == "Default", - |(width, height)| value == &format!("{width}x{height}"), - ) - }) + .position(|choice| choice.resolution() == self.draft.resolution) .unwrap_or(0), }; } @@ -246,7 +279,11 @@ impl State { .map(ToString::to_string) .collect(), ChoicePicker::Java => self.java_picker.labels(), - ChoicePicker::Resolution => resolution_values(self.draft.resolution), + ChoicePicker::Resolution => self + .resolution_choices() + .iter() + .map(ResolutionChoice::label) + .collect(), } } @@ -295,16 +332,17 @@ impl State { } } Some(ChoicePicker::Resolution) => { - let selected = resolution_values(self.draft.resolution) + let selected = self + .resolution_choices() .get(self.choice_index) .cloned() - .unwrap_or_else(|| "Default".to_owned()); - match selected.as_str() { - "Default" => self.draft.resolution = None, - "Custom…" => { + .unwrap_or(ResolutionChoice::Default); + match selected { + ResolutionChoice::Default => self.draft.resolution = None, + ResolutionChoice::Custom => { self.editing = Some(new_text_area(vec![self.value(7)])); } - value => self.draft.resolution = parse_resolution(value).ok(), + choice => self.draft.resolution = choice.resolution(), } } None => {} @@ -462,6 +500,10 @@ impl State { self.error = None; } + fn resolution_choices(&self) -> Vec { + resolution_choices(self.draft.resolution, &self.display_resolutions) + } + fn handle_picker_key(&mut self, key: &KeyEvent) { self.initialize_picker_index(); if self.picker_search.active { @@ -597,9 +639,6 @@ impl State { self.adjust_selected_memory(true); } KeyCode::Enter => self.begin_edit(), - KeyCode::Char('c') if matches!(self.selected, 4 | 5) => { - self.editing = Some(new_text_area(vec![self.effective_memory(self.selected)])); - } KeyCode::Char('r') if matches!(self.selected, 4 | 5) => { self.set_memory(self.selected, None); } @@ -647,27 +686,36 @@ fn runtime_label(config: &InstanceConfig) -> String { } } -fn resolution_values(current: Option<(u32, u32)>) -> Vec { - let mut values = [ - "Default", - "854x480", - "1280x720", - "1600x900", - "1920x1080", - "2560x1440", - "3840x2160", - "Custom…", - ] - .into_iter() - .map(str::to_owned) - .collect::>(); - if let Some((width, height)) = current { - let current = format!("{width}x{height}"); - if !values.contains(¤t) { - values.insert(values.len() - 1, current); +fn resolution_choices( + current: Option<(u32, u32)>, + displays: &[DisplayResolution], +) -> Vec { + let mut choices = vec![ResolutionChoice::Default]; + choices.extend(displays.iter().cloned().map(ResolutionChoice::Display)); + for (width, height) in [ + (854, 480), + (1280, 720), + (1600, 900), + (1920, 1080), + (2560, 1440), + (3840, 2160), + ] { + if !choices + .iter() + .any(|choice| choice.resolution() == Some((width, height))) + { + choices.push(ResolutionChoice::Preset(width, height)); } } - values + if let Some((width, height)) = current + && !choices + .iter() + .any(|choice| choice.resolution() == Some((width, height))) + { + choices.push(ResolutionChoice::Configured(width, height)); + } + choices.push(ResolutionChoice::Custom); + choices } fn new_text_area(lines: Vec) -> TextArea<'static> { @@ -693,8 +741,13 @@ pub fn popup_rect(area: Rect, state: &State) -> Rect { } else { 11 + u16::from(state.error.is_some()) }; + let width = match state.choice_picker { + Some(ChoicePicker::Java) => 72, + Some(ChoicePicker::Resolution) => 64, + _ => 58, + }; area.centered( - Constraint::Percentage(58), + Constraint::Percentage(width), Constraint::Length(height.min(area.height.saturating_sub(4))), ) } @@ -731,7 +784,7 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { } else if matches!(state.selected, 4 | 5) { super::keybind_line(&[ ("h/l", " adjust"), - ("c", " custom"), + ("Enter", " exact"), ("r", " default"), ("s", " save"), ("Esc", " back"), @@ -842,9 +895,13 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { Style::default().fg(theme.accent()), ), Span::styled( - format!("{label:<18}"), + format!("{label:<16}"), Style::default().fg(theme.text_dim()), ), + Span::styled( + if dirty { "* " } else { " " }, + Style::default().fg(theme.accent()), + ), Span::styled( value, Style::default() @@ -861,10 +918,6 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { Modifier::empty() }), ), - Span::styled( - if dirty { " *" } else { "" }, - Style::default().fg(theme.accent()), - ), ]; if index == 6 && !editing { if state.draft.jvm_args.is_empty() { @@ -875,20 +928,27 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { } else { for (argument_index, argument) in state.draft.jvm_args.iter().enumerate() { if argument_index > 0 { - spans.push(Span::styled(" ", Style::default().fg(theme.text_dim()))); + spans.push(Span::raw(" ")); } spans.push(Span::styled( - argument.clone(), - Style::default().fg(if selected { - theme.accent() - } else { - theme.text() - }), + format!(" {argument} "), + Style::default() + .fg(if selected { + theme.accent() + } else { + theme.text() + }) + .bg(theme.background()) + .add_modifier(Modifier::BOLD), )); } } } - Line::from(spans) + Line::from(spans).style(Style::default().bg(if selected { + theme.stripe() + } else { + theme.surface() + })) } fn field_dirty(state: &State, index: usize) -> bool { @@ -908,7 +968,6 @@ fn field_dirty(state: &State, index: usize) -> bool { fn render_choice_picker(frame: &mut Frame, area: Rect, state: &State) { let theme = THEME.as_ref(); - let values = state.choice_values(); let mut list_area = area; if state.choice_picker == Some(ChoicePicker::Java) && let Some(status) = state.java_picker.status() @@ -924,18 +983,74 @@ fn render_choice_picker(frame: &mut Frame, area: Rect, state: &State) { list_area.y = list_area.y.saturating_add(1); list_area.height = list_area.height.saturating_sub(1); } - let items = values - .iter() - .map(|value| { - ListItem::new(Line::from(Span::styled( - value.clone(), - Style::default().fg(theme.text()), - ))) - }) - .collect(); + let items = match state.choice_picker { + Some(ChoicePicker::Java) => state.java_picker.items(), + Some(ChoicePicker::Resolution) => resolution_items(&state.resolution_choices()), + _ => state + .choice_values() + .iter() + .map(|value| { + ListItem::new(Line::from(Span::styled( + value.clone(), + Style::default().fg(theme.text()), + ))) + }) + .collect(), + }; super::select_list::render(items, state.choice_index, list_area, frame.buffer_mut()); } +fn resolution_items(choices: &[ResolutionChoice]) -> Vec> { + let theme = THEME.as_ref(); + choices + .iter() + .map(|choice| { + let mut spans = vec![Span::styled( + choice.label(), + Style::default().fg(theme.text()), + )]; + match choice { + ResolutionChoice::Default => { + spans.extend([Span::raw(" "), badge(" Inherit ", theme.info())]); + } + ResolutionChoice::Display(display) => { + spans.extend([ + Span::raw(" "), + badge( + if display.primary { + " Primary " + } else { + " Current " + }, + if display.primary { + theme.success() + } else { + theme.info() + }, + ), + ]); + if !display.name.is_empty() { + spans.push(Span::styled( + format!(" {}", display.name), + Style::default().fg(theme.text_dim()), + )); + } + } + ResolutionChoice::Preset(_, _) => { + spans.extend([Span::raw(" "), badge(" Preset ", theme.info())]); + } + ResolutionChoice::Configured(_, _) => { + spans.extend([Span::raw(" "), badge(" Configured ", theme.success())]); + } + ResolutionChoice::Custom => { + spans.extend([Span::raw(" "), badge(" Manual ", theme.warning())]); + } + } + ListItem::new(Line::from(spans)) + }) + .collect() +} + fn render_version_picker(frame: &mut Frame, area: Rect, state: &State) { let theme = THEME.as_ref(); let status = match state.picker { @@ -1055,7 +1170,7 @@ mod tests { config.memory_min = Some("512M".to_owned()); let mut state = State::new(&config, temp.path()); state.selected = 4; - state.handle_key(&KeyEvent::from(KeyCode::Char('c'))); + state.handle_key(&KeyEvent::from(KeyCode::Enter)); state.handle_key(&KeyEvent::from(KeyCode::Left)); state.handle_key(&KeyEvent::from(KeyCode::Char('0'))); state.handle_key(&KeyEvent::from(KeyCode::Enter)); @@ -1159,10 +1274,12 @@ mod tests { let mut state = State::new(&instance(), temp.path()); state.selected = 4; - state.begin_edit(); - assert!(state.editing.is_none()); + state.handle_key(&KeyEvent::from(KeyCode::Char('l'))); assert!(state.draft.memory_min.is_some()); + state.begin_edit(); + assert!(state.editing.is_some()); + state.editing = None; state.selected = 6; state.begin_edit(); assert!(state.editing.is_some()); @@ -1178,9 +1295,10 @@ mod tests { state.selected = 7; state.begin_edit(); assert_eq!(state.choice_picker, Some(ChoicePicker::Resolution)); - state.choice_index = resolution_values(state.draft.resolution) + state.choice_index = state + .resolution_choices() .iter() - .position(|value| value == "1920x1080") + .position(|choice| choice.resolution() == Some((1920, 1080))) .unwrap(); state.apply_choice(); assert_eq!(state.draft.resolution, Some((1920, 1080))); @@ -1209,4 +1327,25 @@ mod tests { assert!(matches!(desktop.as_str(), "enabled" | "disabled")); assert!(!desktop.contains('●') && !desktop.contains('○')); } + + #[test] + fn detected_resolutions_are_listed_before_presets_without_duplicates() { + let displays = vec![DisplayResolution { + width: 1920, + height: 1080, + name: "Primary display".to_owned(), + primary: true, + }]; + + let choices = resolution_choices(None, &displays); + + assert!(matches!(choices[1], ResolutionChoice::Display(_))); + assert_eq!( + choices + .iter() + .filter(|choice| choice.resolution() == Some((1920, 1080))) + .count(), + 1 + ); + } } diff --git a/src/tui/widgets/popups/select_list.rs b/src/tui/widgets/popups/select_list.rs index e69cc66..68938ed 100644 --- a/src/tui/widgets/popups/select_list.rs +++ b/src/tui/widgets/popups/select_list.rs @@ -27,6 +27,7 @@ pub(crate) fn render(items: Vec>, selected: usize, area: Rect, buff .highlight_style( Style::default() .fg(THEME.as_ref().accent()) + .bg(THEME.as_ref().stripe()) .add_modifier(Modifier::BOLD), ) .highlight_symbol(Span::styled( diff --git a/src/tui/widgets/popups/settings_controls.rs b/src/tui/widgets/popups/settings_controls.rs index e659bfc..05f8d62 100644 --- a/src/tui/widgets/popups/settings_controls.rs +++ b/src/tui/widgets/popups/settings_controls.rs @@ -5,7 +5,13 @@ use std::sync::{Arc, Mutex}; -use ratatui::{Frame, layout::Rect, style::Style, text::Span, widgets::Gauge}; +use ratatui::{ + Frame, + layout::Rect, + style::{Color, Modifier, Style}, + text::{Line, Span}, + widgets::{LineGauge, ListItem, Paragraph}, +}; use crate::{ config::theme::THEME, instance::java::JavaInstallation, tui::widgets::popups::LoadState, @@ -31,6 +37,14 @@ pub(crate) enum JavaChoice { Custom, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct DisplayResolution { + pub width: u32, + pub height: u32, + pub name: String, + pub primary: bool, +} + impl JavaPicker { pub(crate) fn new() -> Self { Self { @@ -121,6 +135,59 @@ impl JavaPicker { .collect() } + pub(crate) fn items(&self) -> Vec> { + let theme = THEME.as_ref(); + let installations = match &*self + .load + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + { + LoadState::Loaded(installations) => installations.clone(), + _ => Vec::new(), + }; + self.choices() + .into_iter() + .map(|choice| match choice { + JavaChoice::Automatic => ListItem::new(Line::from(vec![ + Span::styled("Automatic", Style::default().fg(theme.text())), + Span::raw(" "), + badge(" Auto ", theme.info()), + Span::styled( + format!(" {}", self.detected), + Style::default().fg(theme.text_dim()), + ), + ])), + JavaChoice::Installation(path) => { + let installation = installations + .iter() + .find(|installation| installation.path.to_string_lossy() == path); + let version = installation + .and_then(|installation| installation.version.as_deref()) + .map_or_else(|| "Java".to_owned(), |version| format!("Java {version}")); + let current = self.current.as_deref() == Some(path.as_str()); + ListItem::new(Line::from(vec![ + Span::styled(version, Style::default().fg(theme.text())), + Span::raw(" "), + badge( + if current { " Current " } else { " Detected " }, + if current { + theme.success() + } else { + theme.info() + }, + ), + Span::styled(format!(" {path}"), Style::default().fg(theme.text_dim())), + ])) + } + JavaChoice::Custom => ListItem::new(Line::from(vec![ + Span::styled("Custom path…", Style::default().fg(theme.text())), + Span::raw(" "), + badge(" Manual ", theme.warning()), + ])), + }) + .collect() + } + pub(crate) fn status(&self) -> Option> { match &*self .load @@ -217,27 +284,100 @@ pub(crate) fn render_memory_gauge( .position(|step| memory_kib(step).is_some_and(|amount| current <= amount)) .unwrap_or(MEMORY_STEPS.len() - 1); let ratio = (index + 1) as f64 / MEMORY_STEPS.len() as f64; - let gauge = Gauge::default() + let value_width = 17.min(area.width); + let value_area = Rect { + width: value_width, + ..area + }; + let line_area = Rect { + x: area.x.saturating_add(value_width).saturating_add(1), + width: area.width.saturating_sub(value_width.saturating_add(1)), + ..area + }; + let value_style = if selected { + Style::default() + .fg(theme.background()) + .bg(theme.accent()) + .add_modifier(Modifier::BOLD) + } else { + Style::default().fg(theme.text()).bg(theme.background()) + }; + frame.render_widget( + Paragraph::new(Line::from(Span::styled(format!(" {label} "), value_style))), + value_area, + ); + + let gauge = LineGauge::default() .ratio(ratio) - .use_unicode(true) - .label(Span::styled( - label, - Style::default().fg(if selected { - theme.text() - } else { - theme.text_dim() - }), - )) - .gauge_style( - Style::default() - .fg(if selected { + .label("") + .filled_symbol("━") + .unfilled_symbol("─") + .filled_style(Style::default().fg(if selected { + theme.accent() + } else { + theme.text_dim() + })) + .unfilled_style(Style::default().fg(theme.border())); + frame.render_widget(gauge, line_area); + if line_area.width > 0 { + let thumb_offset = ((line_area.width.saturating_sub(1)) as f64 * ratio).round() as u16; + frame.render_widget( + Paragraph::new(Span::styled( + "◆", + Style::default().fg(if selected { theme.accent() } else { theme.text_dim() - }) - .bg(theme.background()), + }), + )), + Rect { + x: line_area.x.saturating_add(thumb_offset), + width: 1, + ..line_area + }, ); - frame.render_widget(gauge, area); + } +} + +pub(crate) fn display_resolutions() -> Vec { + let Ok(displays) = display_info::DisplayInfo::all() else { + return Vec::new(); + }; + let mut resolutions = Vec::::new(); + for display in displays { + if display.width == 0 || display.height == 0 { + continue; + } + if let Some(existing) = resolutions.iter_mut().find(|resolution| { + resolution.width == display.width && resolution.height == display.height + }) { + existing.primary |= display.is_primary; + continue; + } + let name = if display.friendly_name.is_empty() { + display.name + } else { + display.friendly_name + }; + resolutions.push(DisplayResolution { + width: display.width, + height: display.height, + name, + primary: display.is_primary, + }); + } + resolutions.sort_by_key(|resolution| !resolution.primary); + resolutions +} + +pub(crate) fn badge(text: &'static str, color: Color) -> Span<'static> { + Span::styled( + text, + Style::default() + .fg(THEME.as_ref().background()) + .bg(color) + .add_modifier(Modifier::BOLD), + ) } #[cfg(test)]