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 scaling #69

Merged
merged 1 commit into from
Dec 8, 2022
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
12 changes: 9 additions & 3 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use image::{Pixel, RgbaImage};
pub struct Font {
fonts: Vec<fontdue::Font>,
layout: RefCell<Layout>,
scale: f32,
size: f32,
scale: i32,
glyph_cache: RefCell<HashMap<GlyphRasterConfig, (Metrics, Vec<u8>)>>,
}

Expand Down Expand Up @@ -49,11 +50,16 @@ impl Font {
Ok(Font {
fonts: font_data,
layout: RefCell::new(Layout::new(CoordinateSystem::PositiveYDown)),
scale: size,
size,
scale: 1,
glyph_cache: RefCell::new(HashMap::new()),
})
}

pub fn set_scale(&mut self, scale: i32) {
self.scale = scale;
}

fn render_glyph(&self, conf: GlyphRasterConfig) -> (Metrics, Vec<u8>) {
let mut glyph_cache = self.glyph_cache.borrow_mut();
if let Some(bitmap) = glyph_cache.get(&conf) {
Expand Down Expand Up @@ -90,7 +96,7 @@ impl Font {
}
layout.append(
&self.fonts,
&TextStyle::new(&c.to_string(), self.scale, font_index),
&TextStyle::new(&c.to_string(), self.size * self.scale as f32, font_index),
);
}

Expand Down
26 changes: 17 additions & 9 deletions src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use smithay_client_toolkit::{
get_surface_scale_factor,
reexports::{
calloop,
client::protocol::{
Expand All @@ -25,7 +26,7 @@ use smithay_clipboard::Clipboard;

use log::*;
use std::cell::Cell;
use std::io::{BufWriter, ErrorKind, Seek, SeekFrom, Write};
use std::io::{BufWriter, ErrorKind, Seek, Write};
use std::rc::Rc;

use image::{Pixel, Rgba, RgbaImage};
Expand Down Expand Up @@ -92,7 +93,7 @@ impl Surface {
layer_surface.ack_configure(serial);
next_render_event_handle.set(Some(RenderEvent::Configure { width, height }));
}
(_, _) => {}
_ => todo!(),
}
});

Expand All @@ -108,11 +109,11 @@ impl Surface {
}
}

pub fn draw(&mut self, mut image: RgbaImage) -> Result<(), std::io::Error> {
pub fn draw(&mut self, mut image: RgbaImage, scale: i32) -> Result<(), std::io::Error> {
if let Some(pool) = self.pools.pool() {
let stride = 4 * self.dimensions.0 as i32;
let width = self.dimensions.0 as i32;
let height = self.dimensions.1 as i32;
let width = self.dimensions.0 as i32 * scale;
let height = self.dimensions.1 as i32 * scale;
let stride = 4 * width;

// First make sure the pool is the right size
pool.resize((stride * height) as usize)?;
Expand All @@ -125,7 +126,7 @@ impl Surface {
});

// Write the color to all bytes of the pool
pool.seek(SeekFrom::Start(0))?;
pool.rewind()?;
{
let mut writer = BufWriter::new(&mut *pool);
writer.write_all(image.as_raw())?;
Expand All @@ -134,8 +135,7 @@ impl Surface {

// Attach the buffer to the surface and mark the entire surface as damaged
self.surface.attach(Some(&buffer), 0, 0);
self.surface
.damage_buffer(0, 0, width as i32, height as i32);
self.surface.damage_buffer(0, 0, width, height);

// Finally, commit the surface
self.surface.commit();
Expand All @@ -147,6 +147,14 @@ impl Surface {
))
}
}

pub fn get_scale(&self) -> i32 {
get_surface_scale_factor(&self.surface)
}

pub fn set_scale(&mut self, scale: i32) {
self.surface.set_buffer_scale(scale);
}
}

impl Drop for Surface {
Expand Down
47 changes: 23 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ async fn run() -> Result<Option<JoinHandle<()>>, Box<dyn Error>> {
match surface.next_render_event.take() {
Some(RenderEvent::Closed) => break,
Some(RenderEvent::Configure { width, height }) => {
need_redraw = surface.set_dimensions(width, height);
need_redraw = true;
surface.set_dimensions(width, height);
}
None => {}
}
Expand Down Expand Up @@ -232,18 +233,26 @@ async fn run() -> Result<Option<JoinHandle<()>>, Box<dyn Error>> {
if need_redraw {
need_redraw = false;

let mut img = ImageBuffer::from_pixel(
surface.dimensions.0,
surface.dimensions.1,
config.colors.background.to_rgba(),
// adjust all components for Hidpi
let scale = surface.get_scale();
surface.set_scale(scale);
font.set_scale(scale);
let (width, height) = (
surface.dimensions.0 * scale as u32,
surface.dimensions.1 * scale as u32,
);
let padding = config.padding * scale as u32;
let font_size = config.font_size * scale as f32;

let mut img =
ImageBuffer::from_pixel(width, height, config.colors.background.to_rgba());
let prompt_width = if !config.prompt.is_empty() {
let (width, _) = font.render(
&config.prompt,
&config.colors.prompt,
&mut img,
config.padding,
config.padding,
padding,
padding,
);
width
} else {
Expand All @@ -256,20 +265,13 @@ async fn run() -> Result<Option<JoinHandle<()>>, Box<dyn Error>> {
} else {
&config.colors.text_query
};
font.render(
query,
color,
&mut img,
config.padding + prompt_width,
config.padding,
);
font.render(query, color, &mut img, padding + prompt_width, padding);
}

let spacer = (1.5 * config.font_size) as u32;
let max_entries = ((surface.dimensions.1 - 2 * config.padding - spacer) as f32
/ (config.font_size * 1.2)) as usize;
let spacer = (1.5 * font_size) as u32;
let max_entries = ((height - 2 * padding - spacer) as f32 / (font_size * 1.2)) as usize;
let offset = if selection > (max_entries / 2) {
(selection - max_entries / 2) as usize
selection - max_entries / 2
} else {
0
};
Expand All @@ -289,15 +291,12 @@ async fn run() -> Result<Option<JoinHandle<()>>, Box<dyn Error>> {
&matched.name,
color,
&mut img,
config.padding,
(config.padding
+ spacer
+ (i - offset) as u32 * (config.font_size * 1.2) as u32)
as u32,
padding,
padding + spacer + (i - offset) as u32 * (font_size * 1.2) as u32,
);
}

match surface.draw(img) {
match surface.draw(img, scale) {
Ok(_) => {}
Err(e) => {
error!("{}", e);
Expand Down