From 47beb1bbaffcd71983d7d3cf1cecff9963e59322 Mon Sep 17 00:00:00 2001 From: 409 Date: Wed, 22 May 2024 03:23:50 +0200 Subject: [PATCH] feat: optional display parameter --- src/arguments.rs | 3 +++ src/config.rs | 1 + src/main.rs | 1 + src/runner/mod.rs | 43 ++++++++++++++++++++++++++++++++++++------- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/src/arguments.rs b/src/arguments.rs index 8a13880..2a3bb6e 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -31,4 +31,7 @@ pub struct Arguments { #[arg(long, help = "The spacing between items", default_value_t = LINE_SPACING)] pub line_spacing: u16, + + #[arg(short, long, help = "The index of the target display")] + pub display: Option, } diff --git a/src/config.rs b/src/config.rs index 519635f..50ab37a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -17,4 +17,5 @@ pub struct RunnerMenuSettings { pub rows: u16, pub font_size: u16, pub line_spacing: u16, + pub display_index: Option, } diff --git a/src/main.rs b/src/main.rs index a439d2c..047860b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,6 +31,7 @@ fn main() -> Result<(), Box> { rows: args.rows, font_size: args.font_size, line_spacing: args.line_spacing, + display_index: args.display, }, ); diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 0586fa3..6e3f453 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -1,3 +1,4 @@ +use core::panic; use std::time::Duration; use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; @@ -7,7 +8,7 @@ use sdl2::{ rect::Rect, render::Canvas, ttf, - video::{DisplayMode, Window}, + video::Window, Sdl, }; @@ -33,7 +34,8 @@ impl Runner { let ttf = ttf::init().expect("Error creating SDL TTF context"); - let window_height: u32; + let (window_width, window_height): (u32, u32); + window_width = 480; { let font_path = String::from("/usr/share/fonts/OTF/GeistMonoNerdFontMono-Regular.otf"); @@ -51,17 +53,44 @@ impl Runner { let video = context.video().expect("Error initializing SDL video"); + let (mut window_x, mut window_y): (i32, i32) = (0, 0); + + match settings.display_index { + Some(display) => { + if (display as i32).lt(&video + .num_video_displays() + .expect("Error getting number of displays")) + { + let bounds = video + .display_bounds(display.into()) + .expect(&format!("Error getting bounds for display {}", display)); + + window_x = bounds.x() + (bounds.width().div_euclid(2) as i32) + - window_width.div_euclid(2) as i32; + window_y = bounds.y() + (bounds.height().div_euclid(2) as i32) + - window_height.div_euclid(2) as i32; + } + } + None => {} + } + let window = video - .window("Practical runner", 480, window_height) - .position_centered() + .window("Practical runner", window_width, window_height) .borderless() .always_on_top() .build() .expect("Error creating window"); - let window_size = window.size(); + let mut canvas = window.into_canvas().build().expect("Error creating canvas"); - let canvas = window.into_canvas().build().expect("Error creating canvas"); + canvas.present(); + if window_x.ne(&0) || window_y.ne(&0) { + canvas.window_mut().set_position( + sdl2::video::WindowPos::Positioned(window_x), + sdl2::video::WindowPos::Positioned(window_y), + ); + } + canvas.window_mut().raise(); let mut cloned_executables = executables.clone(); cloned_executables.sort_by(|a, b| a.to_lowercase().cmp(&b.to_lowercase())); @@ -73,7 +102,7 @@ impl Runner { canvas, input: String::from(""), ttf, - window_size, + window_size: (window_width, window_height), settings, } }