Skip to content

Commit

Permalink
feat: Add window position and size configuration options.
Browse files Browse the repository at this point in the history
formating
  • Loading branch information
kokoISnoTarget committed Mar 27, 2024
1 parent d0b6b1f commit a3699da
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 5 deletions.
10 changes: 10 additions & 0 deletions inlyne.default.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ theme = "Auto"
# lines-to-scroll = 4.5
lines-to-scroll = 3.0

# Position of the window
# [position]
# x = 500
# y = 200

# Geomertry/Size of the window in pixels
# [size]
# width = 600
# height = 500

# The light and dark themes can be customized as well
# Both the light and dark theme colors can be fully customized
[dark-theme]
Expand Down
18 changes: 15 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use winit::event::{
ElementState, Event, KeyboardInput, ModifiersState, MouseButton, MouseScrollDelta, WindowEvent,
};
use winit::event_loop::{ControlFlow, EventLoop, EventLoopBuilder};
use winit::window::{CursorIcon, Window};
use winit::window::{CursorIcon, Window, WindowBuilder};

pub enum InlyneEvent {
LoadedImage(String, Arc<Mutex<Option<ImageData>>>),
Expand Down Expand Up @@ -156,8 +156,20 @@ impl Inlyne {
let file_path = opts.history.get_path().to_owned();

let event_loop = EventLoopBuilder::<InlyneEvent>::with_user_event().build();
let window = Arc::new(Window::new(&event_loop).unwrap());
window.set_title(&utils::format_title(&file_path));

let window = {
let mut wb = WindowBuilder::new().with_title(&utils::format_title(&file_path));

if let Some(ref pos) = opts.position {
wb = wb.with_position(winit::dpi::PhysicalPosition::new(pos.x, pos.y))
}
if let Some(ref size) = opts.size {
wb = wb.with_inner_size(winit::dpi::PhysicalSize::new(size.width, size.height))
}

Arc::new(wb.build(&event_loop).unwrap())
};

let renderer = pollster::block_on(Renderer::new(
&window,
opts.theme.clone(),
Expand Down
11 changes: 10 additions & 1 deletion src/opts/cli.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::{Position, Size};
use clap::{
builder::PossibleValue, command, value_parser, Args as ClapArgs, Parser, Subcommand, ValueEnum,
};
Expand Down Expand Up @@ -68,7 +69,7 @@ pub enum Commands {
Config(ConfigCmd),
}

/// View markdown a file with inlyne
/// View a markdown file with inlyne
#[derive(ClapArgs, PartialEq, Debug, Clone, Default)]
#[command(arg_required_else_help(true))]
pub struct View {
Expand All @@ -91,6 +92,14 @@ pub struct View {
/// Maximum width of page in pixels
#[arg(short = 'w', long = "page-width")]
pub page_width: Option<f32>,

/// Position of the opened window <x>,<y>
#[arg(short = 'p', long = "win-pos", value_parser = value_parser!(Position))]
pub position: Option<Position>,

/// Size of the opened window <width>x<height>
#[arg(short = 'g', long = "win-size", value_parser = value_parser!(Size))]
pub size: Option<Size>,
}

/// Configuration related things
Expand Down
50 changes: 50 additions & 0 deletions src/opts/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fs::{create_dir_all, read_to_string};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use super::ThemeType;
use crate::color;
Expand Down Expand Up @@ -88,6 +89,53 @@ pub struct DebugSection {
pub metrics: Option<MetricsExporter>,
}

#[derive(Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Position {
pub x: i32,
pub y: i32,
}

impl FromStr for Position {
type Err = &'static str;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = input.split(',').collect();
if parts.len() != 2 {
return Err("Invalid format for Position: expected format <x>,<y>");
}
let x = parts[0]
.parse::<i32>()
.map_err(|_| "Invalid x-coordinate: not a valid integer")?;
let y = parts[1]
.parse::<i32>()
.map_err(|_| "Invalid y-coordinate: not a valid integer")?;
Ok(Position { x, y })
}
}

#[derive(Deserialize, Clone, Debug, Default, PartialEq)]
pub struct Size {
pub width: u32,
pub height: u32,
}
impl FromStr for Size {
type Err = &'static str;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = input.split('x').collect();
if parts.len() != 2 {
return Err("Invalid format for Size: expected format <width>x<height>");
}
let width = parts[0]
.parse::<u32>()
.map_err(|_| "Invalid width: not a valid integer")?;
let height = parts[1]
.parse::<u32>()
.map_err(|_| "Invalid height: not a valid integer")?;
Ok(Size { width, height })
}
}

#[derive(Deserialize, Debug, Default, PartialEq)]
#[serde(default, rename_all = "kebab-case")]
pub struct Config {
Expand All @@ -100,6 +148,8 @@ pub struct Config {
pub font_options: Option<FontOptions>,
pub keybindings: KeybindingsSection,
pub debug: DebugSection,
pub position: Option<Position>,
pub size: Option<Size>,
}

impl Config {
Expand Down
15 changes: 14 additions & 1 deletion src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use std::path::Path;

use crate::color;
pub use cli::{Cli, Commands, ConfigCmd, ThemeType, View};
pub use config::{Config, DebugSection, FontOptions, KeybindingsSection, MetricsExporter};
pub use config::{
Config, DebugSection, FontOptions, KeybindingsSection, MetricsExporter, Position, Size,
};

use crate::history::History;
use anyhow::Result;
Expand Down Expand Up @@ -52,6 +54,8 @@ pub struct Opts {
pub keybindings: KeybindingsSection,
pub color_scheme: Option<ResolvedTheme>,
pub metrics: Option<MetricsExporter>,
pub position: Option<Position>,
pub size: Option<Size>,
}

impl Opts {
Expand Down Expand Up @@ -93,6 +97,8 @@ impl Opts {
font_options,
keybindings,
debug,
size,
position,
} = config;

let View {
Expand All @@ -101,6 +107,8 @@ impl Opts {
scale: args_scale,
config: _,
page_width: args_page_width,
size: v_size,
position: v_position,
} = args;

let DebugSection { metrics } = debug;
Expand All @@ -126,6 +134,9 @@ impl Opts {
let page_width = args_page_width.or(config_page_width);
let lines_to_scroll = lines_to_scroll.into();

let position = v_position.or(position);
let size = v_size.or(size);

Ok(Self {
history: History::new(file_path),
theme,
Expand All @@ -136,6 +147,8 @@ impl Opts {
keybindings,
color_scheme: resolved_theme,
metrics,
position,
size,
})
}

Expand Down

0 comments on commit a3699da

Please sign in to comment.