Skip to content

Commit 92087b7

Browse files
committed
Windows re-open in the same position
1 parent c04a957 commit 92087b7

File tree

12 files changed

+167
-29
lines changed

12 files changed

+167
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ members = [
2828
"viz-udp-app",
2929
"viz-udp-app-lib",
3030
"widgets",
31+
"window-utils",
3132
]

viz-udp-app-lib/src/blink.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ impl Default for Config {
2222
width: 100,
2323
height: 100,
2424
red: 127,
25-
green: 0,
26-
blue: 0,
25+
green: 127,
26+
blue: 127,
2727
}
2828
}
2929
}

viz-udp-app/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ line_2d = "0.5"
1616
clap = { version = "4", features = ["derive"] }
1717
caw_viz_udp_app_lib = { version = "0.1", path = "../viz-udp-app-lib" }
1818
lazy_static = "1.5"
19+
log = "0.4"
20+
caw_window_utils = { version = "0.1", path = "../window-utils" }

viz-udp-app/src/main.rs

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use caw_viz_udp_app_lib::{
55
blink,
66
oscilloscope::{self, OscilloscopeStyle},
77
};
8+
use caw_window_utils::persisten_window_position;
89
use clap::{Parser, Subcommand, ValueEnum};
910
use lazy_static::lazy_static;
1011
use line_2d::Coord;
@@ -85,11 +86,11 @@ struct BlinkCommand {
8586
width: u32,
8687
#[arg(long, default_value_t = 100)]
8788
height: u32,
88-
#[arg(long, short, default_value_t = 0)]
89+
#[arg(long, short, default_value_t = 127)]
8990
red: u8,
9091
#[arg(long, short, default_value_t = 127)]
9192
green: u8,
92-
#[arg(long, short, default_value_t = 0)]
93+
#[arg(long, short, default_value_t = 127)]
9394
blue: u8,
9495
}
9596

@@ -141,9 +142,25 @@ struct App {
141142
server: String,
142143
canvas: Canvas<Window>,
143144
event_pump: EventPump,
145+
title: String,
144146
}
145147

146148
impl App {
149+
fn handle_event_common(event: Event, title: &str) {
150+
match event {
151+
Event::Quit { .. } => std::process::exit(0),
152+
Event::Window {
153+
win_event: WindowEvent::Moved(x, y),
154+
..
155+
} => {
156+
if let Err(e) = persisten_window_position::save(title, x, y) {
157+
log::warn!("{}", e);
158+
}
159+
}
160+
_ => (),
161+
}
162+
}
163+
147164
fn run_oscilloscope(
148165
mut self,
149166
mut args: OscilloscopeCommand,
@@ -152,8 +169,8 @@ impl App {
152169
let mut scope_state = ScopeState::default();
153170
loop {
154171
for event in self.event_pump.poll_iter() {
172+
Self::handle_event_common(event.clone(), self.title.as_str());
155173
match event {
156-
Event::Quit { .. } => std::process::exit(0),
157174
Event::MouseWheel { y, .. } => {
158175
let ratio = 1.1;
159176
if y > 0 {
@@ -381,10 +398,7 @@ impl App {
381398
let mut prev_mean = 0.0;
382399
loop {
383400
for event in self.event_pump.poll_iter() {
384-
match event {
385-
Event::Quit { .. } => std::process::exit(0),
386-
_ => (),
387-
}
401+
Self::handle_event_common(event.clone(), self.title.as_str());
388402
}
389403
let mut total = 0.0;
390404
let mut count = 0;
@@ -433,17 +447,30 @@ fn main() -> anyhow::Result<()> {
433447
} = Cli::parse();
434448
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
435449
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;
436-
let window = match command {
437-
Command::Oscilloscope(_) => video_subsystem
438-
.window(title.as_str(), command.width(), command.height())
439-
.always_on_top()
440-
.resizable()
441-
.build()?,
442-
Command::Blink(_) => video_subsystem
443-
.window("", command.width(), command.height())
444-
.always_on_top()
445-
.build()?,
450+
let mut window_builder = match command {
451+
Command::Oscilloscope(_) => {
452+
let mut wb = video_subsystem.window(
453+
title.as_str(),
454+
command.width(),
455+
command.height(),
456+
);
457+
wb.resizable();
458+
wb
459+
}
460+
Command::Blink(_) => {
461+
let mut wb =
462+
video_subsystem.window("", command.width(), command.height());
463+
wb.always_on_top();
464+
wb
465+
}
446466
};
467+
match persisten_window_position::load(&title) {
468+
Err(e) => log::warn!("{}", e),
469+
Ok((x, y)) => {
470+
window_builder.position(x, y);
471+
}
472+
}
473+
let window = window_builder.build()?;
447474
let mut canvas = window
448475
.into_canvas()
449476
.target_texture()
@@ -455,6 +482,7 @@ fn main() -> anyhow::Result<()> {
455482
server,
456483
event_pump,
457484
canvas,
485+
title: title.clone(),
458486
};
459487
match command {
460488
Command::Oscilloscope(oscilloscope_command) => {

widgets/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ midly = "0.5"
1818
caw_computer_keyboard = { version = "0.4", path = "../computer-keyboard" }
1919
caw_keyboard = { version = "0.4", path = "../keyboard" }
2020
caw_sdl2 = { version = "0.1", path = "../sdl2" }
21+
caw_window_utils = { version = "0.1", path = "../window-utils" }
2122
itertools = "0.14"
23+
log = "0.4"
2224

2325
[dev-dependencies]
2426
env_logger = "0.11"

widgets/src/button.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ impl Button {
2323
fn handle_events(&mut self) {
2424
for event in self.window.event_pump.poll_iter() {
2525
use sdl2::event::Event;
26+
Window::handle_event_common(
27+
event.clone(),
28+
self.window.title.as_ref(),
29+
);
2630
match event {
27-
Event::Quit { .. } => std::process::exit(0),
2831
Event::MouseButtonDown { .. } => {
2932
self.pressed = true;
3033
}

widgets/src/computer_keyboard.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,11 @@ impl ComputerKeyboard {
7272
fn handle_events(&mut self, buf: &mut Vec<MidiMessage>) {
7373
for event in self.window.event_pump.poll_iter() {
7474
use sdl2::event::Event;
75+
Window::handle_event_common(
76+
event.clone(),
77+
self.window.title.as_ref(),
78+
);
7579
match event {
76-
Event::Quit { .. } => std::process::exit(0),
7780
Event::KeyDown {
7881
scancode: Some(Scancode::Space),
7982
..

widgets/src/knob.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ use crate::window::Window;
22
use anyhow::anyhow;
33
use line_2d::Coord;
44
use midly::num::u7;
5-
use sdl2::{keyboard::Scancode, mouse::MouseButton, pixels::Color, rect::Rect};
5+
use sdl2::{
6+
event::Event, keyboard::Scancode, mouse::MouseButton, pixels::Color,
7+
rect::Rect,
8+
};
69
use std::time::Instant;
710

811
const WIDTH_PX: u32 = 128;
@@ -32,9 +35,11 @@ impl Knob {
3235

3336
fn handle_events(&mut self) {
3437
for event in self.window.event_pump.poll_iter() {
35-
use sdl2::event::Event;
38+
Window::handle_event_common(
39+
event.clone(),
40+
self.window.title.as_ref(),
41+
);
3642
match event {
37-
Event::Quit { .. } => std::process::exit(0),
3843
Event::KeyDown { scancode, .. } => {
3944
let value_01 = match scancode {
4045
Some(Scancode::Grave) => 0.0,

widgets/src/window.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use anyhow::anyhow;
2+
use caw_window_utils::persisten_window_position;
23
use lazy_static::lazy_static;
34
use sdl2::{
45
EventPump,
6+
event::{Event, WindowEvent},
57
pixels::Color,
68
rect::Rect,
79
render::{Canvas, TextureCreator},
@@ -48,10 +50,18 @@ impl Window {
4850
) -> anyhow::Result<Self> {
4951
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
5052
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;
51-
let window = video_subsystem
52-
.window("", width_px, height_px)
53-
.always_on_top()
54-
.build()?;
53+
let mut window_builder =
54+
video_subsystem.window("", width_px, height_px);
55+
window_builder.always_on_top();
56+
if let Some(title) = title {
57+
match persisten_window_position::load(title) {
58+
Err(e) => log::warn!("{}", e),
59+
Ok((x, y)) => {
60+
window_builder.position(x, y);
61+
}
62+
}
63+
}
64+
let window = window_builder.build()?;
5565
let canvas = window
5666
.into_canvas()
5767
.target_texture()
@@ -112,7 +122,26 @@ impl Window {
112122
pub fn width_px(&self) -> u32 {
113123
self.width_px
114124
}
125+
115126
pub fn height_px(&self) -> u32 {
116127
self.height_px
117128
}
129+
130+
pub fn handle_event_common(event: Event, title: Option<&String>) {
131+
match event {
132+
Event::Quit { .. } => std::process::exit(0),
133+
Event::Window {
134+
win_event: WindowEvent::Moved(x, y),
135+
..
136+
} => {
137+
if let Some(title) = title {
138+
if let Err(e) = persisten_window_position::save(title, x, y)
139+
{
140+
log::warn!("{}", e);
141+
}
142+
}
143+
}
144+
_ => (),
145+
}
146+
}
118147
}

widgets/src/xy.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ impl Xy {
2626
fn handle_events(&mut self) {
2727
for event in self.window.event_pump.poll_iter() {
2828
use sdl2::event::Event;
29+
Window::handle_event_common(
30+
event.clone(),
31+
self.window.title.as_ref(),
32+
);
2933
match event {
30-
Event::Quit { .. } => std::process::exit(0),
3134
Event::MouseButtonDown { x, y, .. } => {
3235
self.x_px = x.clamp(0, WIDTH_PX as i32) as u32;
3336
self.y_px = y.clamp(0, HEIGHT_PX as i32) as u32;

0 commit comments

Comments
 (0)