refactor + version update

This commit is contained in:
MeexReay 2024-04-25 18:53:48 +03:00
parent 919241aeac
commit dbbeb4fabb
2 changed files with 364 additions and 281 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "fonotes" name = "fonotes"
version = "0.1.0" version = "1.0.0"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -1,57 +1,45 @@
use std::collections::HashMap;
use std::iter::Zip;
use std::num::NonZeroU32;
use std::ops::Deref;
use std::os::linux::raw::stat;
use send_wrapper::SendWrapper;
use std::sync::mpsc::channel;
use std::cmp; use std::cmp;
use std::num::NonZeroU32;
use fontdue::{Font, FontSettings};
use fontdue::layout::{Layout, CoordinateSystem, TextStyle};
use tiny_skia::{Color, ColorU8, FillRule, Paint, Path, PathBuilder, Pixmap, PixmapPaint, PixmapRef, Rect, Stroke, Transform};
use softbuffer::{Context, Surface};
use arboard::{Clipboard, ImageData}; use arboard::{Clipboard, ImageData};
use fontdue::layout::{CoordinateSystem, Layout, TextStyle};
use fontdue::{Font, FontSettings};
use softbuffer::{Context, Surface};
use tiny_skia::{
Color, FillRule, Paint, PathBuilder, Pixmap, PixmapPaint, Rect,
Transform,
};
use winit::event::{ElementState, Event, KeyEvent, MouseButton, WindowEvent}; use winit::dpi::LogicalSize;
use winit::dpi::PhysicalPosition;
use winit::event::{Event, MouseButton, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, EventLoopBuilder}; use winit::event_loop::{ControlFlow, EventLoop, EventLoopBuilder};
use winit::platform::x11::EventLoopBuilderExtX11; use winit::platform::x11::EventLoopBuilderExtX11;
use winit::window::{Window, WindowBuilder, WindowButtons, WindowId, WindowLevel};
use winit::dpi::{PhysicalPosition, PhysicalSize};
use winit::window::CursorIcon; use winit::window::CursorIcon;
use winit::window::ResizeDirection; use winit::window::ResizeDirection;
use winit::dpi::LogicalSize; use winit::window::{Window, WindowBuilder, WindowButtons, WindowId, WindowLevel};
use rdev::*; use rdev::*;
use std::thread::{self, JoinHandle};
use std::sync::{Arc,Mutex,MutexGuard};
use std::borrow::{BorrowMut, Cow};
use std::cell::{Cell, Ref};
use std::cell::{RefCell, RefMut};
use std::rc::Rc;
use core::slice::IterMut; use core::slice::IterMut;
use std::cell::RefCell;
use std::sync::Arc;
use std::thread;
#[derive(Debug)] #[derive(Debug)]
enum ClipboardContent { enum ClipboardContent {
Image(ImageData<'static>), Image(ImageData<'static>),
Text(String), Text(String),
None None,
} }
fn get_clipboard(clipboard: &mut Clipboard) -> ClipboardContent { fn get_clipboard(clipboard: &mut Clipboard) -> ClipboardContent {
match clipboard.get_image() { match clipboard.get_image() {
Ok(i) => { Ok(i) => ClipboardContent::Image(i.to_owned_img()),
ClipboardContent::Image(i.to_owned_img()) Err(_) => match clipboard.get_text() {
}, Err(e) => { Ok(i) => ClipboardContent::Text(i),
match clipboard.get_text() { Err(_) => ClipboardContent::None,
Ok(i) => { },
ClipboardContent::Text(i)
}, Err(e) => {
ClipboardContent::None
}
}
}
} }
} }
@ -93,13 +81,17 @@ fn render_text(text: String, font_size: f32, font: &Font, color: Color) -> Pixma
let rendered = render_char(ch, font_size, font, color); let rendered = render_char(ch, font_size, font, color);
text_width = gl.x as i32 + gl.width as i32; text_width = gl.x as i32 + gl.width as i32;
if height > max_height { max_height = height; } if height > max_height {
max_height = height;
}
chars.push((rendered, gl.x, gl.y)); chars.push((rendered, gl.x, gl.y));
} }
let mut pixmap = match Pixmap::new(text_width as u32, max_height as u32) { let mut pixmap = match Pixmap::new(text_width as u32, max_height as u32) {
Some(i) => {i}, Some(i) => i,
None => { return Pixmap::new(1, 1).unwrap(); }, None => {
return Pixmap::new(1, 1).unwrap();
}
}; };
let paint = PixmapPaint::default(); let paint = PixmapPaint::default();
@ -108,23 +100,32 @@ fn render_text(text: String, font_size: f32, font: &Font, color: Color) -> Pixma
let pos = (jujuk.1, jujuk.2); let pos = (jujuk.1, jujuk.2);
pixmap.draw_pixmap( pixmap.draw_pixmap(
pos.0 as i32, pos.0 as i32,
pos.1 as i32, pos.1 as i32,
ele.as_ref(), ele.as_ref(),
&paint, &paint,
Transform::identity(), Transform::identity(),
None); None,
);
} }
pixmap pixmap
} }
fn render_text_with_ln(text: String, size: f32, font: &Font, line_height: i32, color: Color) -> Pixmap { fn render_text_with_ln(
text: String,
size: f32,
font: &Font,
line_height: i32,
color: Color,
) -> Pixmap {
let mut width = 0; let mut width = 0;
let mut lines: Vec<Pixmap> = Vec::new(); let mut lines: Vec<Pixmap> = Vec::new();
for ele in text.split("\n") { for ele in text.split("\n") {
let rendered = render_text(ele.to_string(), size, font, color); let rendered = render_text(ele.to_string(), size, font, color);
if rendered.width() > width { width = rendered.width(); } if rendered.width() > width {
width = rendered.width();
}
lines.push(rendered); lines.push(rendered);
} }
let height = (line_height * lines.len() as i32) as i32; let height = (line_height * lines.len() as i32) as i32;
@ -134,7 +135,14 @@ fn render_text_with_ln(text: String, size: f32, font: &Font, line_height: i32, c
let mut y: i32 = 0; let mut y: i32 = 0;
for ele in lines { for ele in lines {
pixmap.draw_pixmap(0, (y + (line_height - ele.height() as i32)) as i32, ele.as_ref(), &paint, Transform::identity(), None); pixmap.draw_pixmap(
0,
(y + (line_height - ele.height() as i32)) as i32,
ele.as_ref(),
&paint,
Transform::identity(),
None,
);
y += line_height; y += line_height;
} }
@ -152,7 +160,8 @@ fn render_image(image: ImageData<'static>) -> Pixmap {
pub trait RemoveElem<T: PartialEq> { pub trait RemoveElem<T: PartialEq> {
fn remove_elem<F>(&mut self, predicate: F) -> Option<T> fn remove_elem<F>(&mut self, predicate: F) -> Option<T>
where F: Fn(&T) -> bool; where
F: Fn(&T) -> bool;
fn remove_value(&mut self, value: &T) -> Option<T>; fn remove_value(&mut self, value: &T) -> Option<T>;
} }
@ -168,17 +177,16 @@ impl<T: PartialEq> RemoveElem<T> for Vec<T> {
} }
fn remove_value(&mut self, value: &T) -> Option<T> { fn remove_value(&mut self, value: &T) -> Option<T> {
self.remove_elem(|e|{e == value}) self.remove_elem(|e| e == value)
} }
} }
struct Note { struct Note {
window: Arc<Window>, window: Arc<Window>,
window_id: WindowId, window_id: WindowId,
context: Context<Arc<Window>>,
surface: Surface<Arc<Window>, Arc<Window>>, surface: Surface<Arc<Window>, Arc<Window>>,
mouse_pos: PhysicalPosition<f64>, mouse_pos: PhysicalPosition<f64>,
clipboard: ClipboardContent clipboard: ClipboardContent,
} }
impl PartialEq for Note { impl PartialEq for Note {
@ -192,8 +200,8 @@ impl Note {
let arc_window = Arc::new(window); let arc_window = Arc::new(window);
let context = Context::new(arc_window.clone()).unwrap(); let context = Context::new(arc_window.clone()).unwrap();
let mut surface = Surface::new(&context, arc_window.clone()).unwrap(); let surface = Surface::new(&context, arc_window.clone()).unwrap();
let mouse_pos: PhysicalPosition<f64> = PhysicalPosition::new(0.0, 0.0); let mouse_pos: PhysicalPosition<f64> = PhysicalPosition::new(0.0, 0.0);
let window_id = arc_window.clone().id(); let window_id = arc_window.clone().id();
@ -201,16 +209,18 @@ impl Note {
Note { Note {
window: arc_window.clone(), window: arc_window.clone(),
window_id, window_id,
context,
surface, surface,
mouse_pos, mouse_pos,
clipboard clipboard,
} }
} }
} }
fn create_event_loop() -> EventLoop<MyUserEvent> { fn create_event_loop() -> EventLoop<MyUserEvent> {
let event_loop: EventLoop<MyUserEvent> = EventLoopBuilder::with_user_event().with_any_thread(true).build().unwrap(); let event_loop: EventLoop<MyUserEvent> = EventLoopBuilder::with_user_event()
.with_any_thread(true)
.build()
.unwrap();
event_loop.set_control_flow(ControlFlow::Poll); event_loop.set_control_flow(ControlFlow::Poll);
event_loop.set_control_flow(ControlFlow::Wait); event_loop.set_control_flow(ControlFlow::Wait);
@ -233,7 +243,7 @@ fn draw_debug_rect<'a>(pixmap: &'a mut Pixmap, x: i32, y: i32, w: i32, h: i32) {
Rect::from_xywh( Rect::from_xywh(
x as f32, x as f32,
y as f32, y as f32,
w as f32, w as f32,
h as f32 h as f32
).unwrap()); ).unwrap());
@ -241,251 +251,317 @@ fn draw_debug_rect<'a>(pixmap: &'a mut Pixmap, x: i32, y: i32, w: i32, h: i32) {
paint.set_color_rgba8(220, 0, 0, 255); paint.set_color_rgba8(220, 0, 0, 255);
pixmap.fill_path( pixmap.fill_path(
&path, &path,
&paint, &paint,
FillRule::EvenOdd, FillRule::EvenOdd,
Transform::identity(), Transform::identity(),
None, None,
); );
} }
fn run_event_loop(event_loop: EventLoop<MyUserEvent>, windows: RefCell<Vec<Note>>) { fn run_event_loop(event_loop: EventLoop<MyUserEvent>, windows: RefCell<Vec<Note>>) {
let font: Font = Font::from_bytes(include_bytes!("../resources/Roboto.ttf") as &[u8], FontSettings::default()).unwrap(); let font: Font = Font::from_bytes(
include_bytes!("../resources/Roboto.ttf") as &[u8],
FontSettings::default(),
)
.unwrap();
event_loop.run(move |event, elwt| { event_loop
let mut windows_local = windows.borrow_mut(); .run(move |event, elwt| {
let mut windows_local = windows.borrow_mut();
match event { match event {
Event::Resumed => {}, Event::Resumed => {}
Event::UserEvent(win) => { Event::UserEvent(win) => {
let mut built = win.window_builder.build(&elwt).unwrap(); let built = win.window_builder.build(&elwt).unwrap();
let mut win = Note::new(built, win.clipboard); let win = Note::new(built, win.clipboard);
windows_local.push(win); windows_local.push(win);
}, }
Event::WindowEvent { window_id, event } => { Event::WindowEvent { window_id, event } => {
let mut win = match get_window(windows_local.iter_mut(), window_id) { let win = match get_window(windows_local.iter_mut(), window_id) {
Some(i) => i, Some(i) => i,
None => {return;}, None => {
}; return;
}
};
match event { match event {
WindowEvent::MouseInput { device_id, state, button } => { WindowEvent::MouseInput {
// dbg!((device_id, state, button)); device_id: _,
state,
button,
} => {
// dbg!((device_id, state, button));
let (width, height) = { let (width, height) = {
let size = win.window.inner_size(); let size = win.window.inner_size();
(size.width as f64, size.height as f64) (size.width as f64, size.height as f64)
}; };
if button == MouseButton::Left && state.is_pressed() { if button == MouseButton::Left && state.is_pressed() {
if win.mouse_pos.x > width - 30.0 && win.mouse_pos.y < 30.0 { // close button if win.mouse_pos.x > width - 30.0 && win.mouse_pos.y < 30.0 {
win.window.set_visible(false); // close button
windows_local.remove_elem(|e| e.window_id == window_id); win.window.set_visible(false);
} else if win.mouse_pos.x < 20.0 && win.mouse_pos.y < 20.0 { // west north windows_local.remove_elem(|e| e.window_id == window_id);
win.window.drag_resize_window(ResizeDirection::NorthWest).unwrap(); } else if win.mouse_pos.x < 20.0 && win.mouse_pos.y < 20.0 {
} else if win.mouse_pos.x < 20.0 && win.mouse_pos.y > height - 20.0 { // west south // west north
win.window.drag_resize_window(ResizeDirection::SouthWest).unwrap(); win.window
} else if win.mouse_pos.x > width - 20.0 && win.mouse_pos.y < 20.0 { // east north .drag_resize_window(ResizeDirection::NorthWest)
win.window.drag_resize_window(ResizeDirection::NorthEast).unwrap(); .unwrap();
} else if win.mouse_pos.x > width - 20.0 && win.mouse_pos.y > height - 20.0 { // east south } else if win.mouse_pos.x < 20.0 && win.mouse_pos.y > height - 20.0
win.window.drag_resize_window(ResizeDirection::SouthEast).unwrap(); {
} else if win.mouse_pos.y < 20.0 && win.mouse_pos.x < 20.0 { // north west // west south
win.window.drag_resize_window(ResizeDirection::NorthWest).unwrap(); win.window
} else if win.mouse_pos.y < 20.0 && win.mouse_pos.x > width - 20.0 { // north east .drag_resize_window(ResizeDirection::SouthWest)
win.window.drag_resize_window(ResizeDirection::NorthEast).unwrap(); .unwrap();
} else if win.mouse_pos.y > height - 20.0 && win.mouse_pos.x < 20.0 { // south west } else if win.mouse_pos.x > width - 20.0 && win.mouse_pos.y < 20.0 {
win.window.drag_resize_window(ResizeDirection::SouthWest).unwrap(); // east north
} else if win.mouse_pos.y > height - 20.0 && win.mouse_pos.x > width - 20.0 { // south east win.window
win.window.drag_resize_window(ResizeDirection::SouthEast).unwrap(); .drag_resize_window(ResizeDirection::NorthEast)
} else if win.mouse_pos.x < 20.0 { // west .unwrap();
win.window.drag_resize_window(ResizeDirection::West).unwrap(); } else if win.mouse_pos.x > width - 20.0
} else if win.mouse_pos.x > width - 20.0 { // east && win.mouse_pos.y > height - 20.0
win.window.drag_resize_window(ResizeDirection::East).unwrap(); {
} else if win.mouse_pos.y > height - 20.0 { // south // east south
win.window.drag_resize_window(ResizeDirection::South).unwrap(); win.window
} else if win.mouse_pos.y < 20.0 { // north .drag_resize_window(ResizeDirection::SouthEast)
win.window.drag_resize_window(ResizeDirection::North).unwrap(); .unwrap();
} else { // else } else if win.mouse_pos.y < 20.0 && win.mouse_pos.x < 20.0 {
win.window.drag_window(); // north west
win.window
.drag_resize_window(ResizeDirection::NorthWest)
.unwrap();
} else if win.mouse_pos.y < 20.0 && win.mouse_pos.x > width - 20.0 {
// north east
win.window
.drag_resize_window(ResizeDirection::NorthEast)
.unwrap();
} else if win.mouse_pos.y > height - 20.0 && win.mouse_pos.x < 20.0
{
// south west
win.window
.drag_resize_window(ResizeDirection::SouthWest)
.unwrap();
} else if win.mouse_pos.y > height - 20.0
&& win.mouse_pos.x > width - 20.0
{
// south east
win.window
.drag_resize_window(ResizeDirection::SouthEast)
.unwrap();
} else if win.mouse_pos.x < 20.0 {
// west
win.window
.drag_resize_window(ResizeDirection::West)
.unwrap();
} else if win.mouse_pos.x > width - 20.0 {
// east
win.window
.drag_resize_window(ResizeDirection::East)
.unwrap();
} else if win.mouse_pos.y > height - 20.0 {
// south
win.window
.drag_resize_window(ResizeDirection::South)
.unwrap();
} else if win.mouse_pos.y < 20.0 {
// north
win.window
.drag_resize_window(ResizeDirection::North)
.unwrap();
} else {
// else
win.window.drag_window().unwrap();
}
} }
} }
}, WindowEvent::CursorMoved {
WindowEvent::CursorMoved { device_id, position } => { device_id: _,
win.mouse_pos = position; position,
} => {
win.mouse_pos = position;
let (width, height) = { let (width, height) = {
let size = win.window.inner_size(); let size = win.window.inner_size();
(size.width as f64, size.height as f64) (size.width as f64, size.height as f64)
}; };
if position.x > width - 30.0 && position.y < 30.0 { if position.x > width - 30.0 && position.y < 30.0 {
win.window.set_cursor_icon(CursorIcon::Pointer); win.window.set_cursor_icon(CursorIcon::Pointer);
} else if position.x < 20.0 || position.x > width - 20.0 { } else if position.x < 20.0 || position.x > width - 20.0 {
win.window.set_cursor_icon(CursorIcon::EwResize) win.window.set_cursor_icon(CursorIcon::EwResize)
} else if position.y < 20.0 || position.y > height - 20.0 { } else if position.y < 20.0 || position.y > height - 20.0 {
win.window.set_cursor_icon(CursorIcon::NsResize) win.window.set_cursor_icon(CursorIcon::NsResize)
} else { } else {
win.window.set_cursor_icon(CursorIcon::Pointer); win.window.set_cursor_icon(CursorIcon::Pointer);
}
} }
}, WindowEvent::RedrawRequested => {
WindowEvent::RedrawRequested => { let (width, height) = {
let (width, height) = { let size = win.window.inner_size();
let size = win.window.inner_size(); (size.width, size.height)
(size.width, size.height) };
}; win.surface
win.surface .resize(
.resize( NonZeroU32::new(width).unwrap(),
NonZeroU32::new(width).unwrap(), NonZeroU32::new(height).unwrap(),
NonZeroU32::new(height).unwrap(), )
) .unwrap();
.unwrap();
let mut pixmap = Pixmap::new(width, height).unwrap();
pixmap.fill(Color::from_rgba8(250, 250, 120, 250));
let mut pixmap_paint = PixmapPaint::default(); let mut pixmap = Pixmap::new(width, height).unwrap();
pixmap.fill(Color::from_rgba8(250, 250, 120, 250));
match &win.clipboard { let pixmap_paint = PixmapPaint::default();
ClipboardContent::Text(t) => {
let win_size: (f32,f32) = (width as f32, height as f32); match &win.clipboard {
let text_table_size: (f32,f32) = { ClipboardContent::Text(t) => {
let pix = render_text_with_ln( let win_size: (f32, f32) = (width as f32, height as f32);
t.to_string(), let text_table_size: (f32, f32) = {
10.0, let pix = render_text_with_ln(
&font, t.to_string(),
12, 10.0,
Color::from_rgba8(255, 0, 0, 255) &font,
12,
Color::from_rgba8(255, 0, 0, 255),
);
(pix.width() as f32 / 10.0, pix.height() as f32 / 10.0)
};
// dbg!(&text_table_size);
let text_size = cmp::min(
(win_size.1 / text_table_size.1) as i32,
(win_size.0 / text_table_size.0) as i32,
) as f32;
let text_pixmap = render_text_with_ln(
t.to_string(),
text_size,
&font,
(text_size * 1.2) as i32,
Color::from_rgba8(255, 0, 0, 255),
); );
(
pix.width() as f32 / 10.0,
pix.height() as f32 / 10.0
)
};
// dbg!(&text_table_size); let text_pos = (
width as i32 / 2 - text_pixmap.width() as i32 / 2,
height as i32 / 2 - text_pixmap.height() as i32 / 2,
);
let text_size = cmp::min( // draw_debug_rect(
(win_size.1 / text_table_size.1) as i32, // &mut pixmap,
(win_size.0 / text_table_size.0) as i32 // text_pos.0 as i32,
) as f32; // text_pos.1 as i32,
// text_pixmap.width() as i32,
// text_pixmap.height() as i32
// );
let mut text_pixmap = render_text_with_ln( pixmap.draw_pixmap(
t.to_string(), text_pos.0,
text_size, text_pos.1,
&font, text_pixmap.as_ref(),
(text_size * 1.2) as i32, &pixmap_paint,
Color::from_rgba8(255, 0, 0, 255) Transform::identity(),
); None,
);
}
ClipboardContent::Image(im) => {
let image_pixmap = render_image(im.clone());
let text_pos = ( let win_size: (f32, f32) = (width as f32, height as f32);
width as i32 / 2 - text_pixmap.width() as i32 / 2, let image_size: (f32, f32) =
height as i32 / 2 - text_pixmap.height() as i32 / 2 (image_pixmap.width() as f32, image_pixmap.height() as f32);
); let image_scale =
(win_size.0 / image_size.0, win_size.1 / image_size.1);
// draw_debug_rect( let image_pos: (i32, i32) = (0, 0);
// &mut pixmap,
// text_pos.0 as i32,
// text_pos.1 as i32,
// text_pixmap.width() as i32,
// text_pixmap.height() as i32
// );
pixmap.draw_pixmap(text_pos.0, text_pos.1, text_pixmap.as_ref(), &pixmap_paint, Transform::identity(), None); pixmap.draw_pixmap(
}, image_pos.0,
ClipboardContent::Image(im) => { image_pos.1,
let mut image_pixmap = render_image(im.clone()); image_pixmap.as_ref(),
&pixmap_paint,
Transform::from_scale(image_scale.0, image_scale.1),
None,
);
}
_ => {}
}
let win_size: (f32,f32) = (width as f32, height as f32); let path = PathBuilder::from_rect(
let image_size: (f32,f32) = (image_pixmap.width() as f32, image_pixmap.height() as f32); Rect::from_xywh(width as f32 - 30.0, 0.0, 30.0, 30.0).unwrap(),
let image_scale = (win_size.0 / image_size.0, win_size.1 / image_size.1); );
let image_pos: (i32, i32) = (0,0); let mut paint = Paint::default();
paint.set_color_rgba8(220, 80, 80, 150);
pixmap.draw_pixmap( pixmap.fill_path(
image_pos.0, &path,
image_pos.1, &paint,
image_pixmap.as_ref(), FillRule::EvenOdd,
&pixmap_paint, Transform::identity(),
Transform::from_scale( None,
image_scale.0, );
image_scale.1),
None); let mut path = PathBuilder::new();
}, path.move_to(width as f32 - 22.5, 2.5);
_ => {}, path.line_to(width as f32 - 2.5, 27.5);
path.line_to(width as f32 - 5.0, 27.5);
path.line_to(width as f32 - 25.0, 2.5);
path.line_to(width as f32 - 22.5, 2.5);
let path = path.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(220, 220, 220, 255);
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
let mut path = PathBuilder::new();
path.move_to(width as f32 - 5.0, 2.5);
path.line_to(width as f32 - 25.0, 27.5);
path.line_to(width as f32 - 22.5, 27.5);
path.line_to(width as f32 - 2.5, 2.5);
path.line_to(width as f32 - 5.0, 2.5);
let path = path.finish().unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
let mut buffer = win.surface.buffer_mut().unwrap();
for index in 0..(width * height) as usize {
buffer[index] = pixmap.data()[index * 4 + 2] as u32
| (pixmap.data()[index * 4 + 1] as u32) << 8
| (pixmap.data()[index * 4] as u32) << 16;
}
buffer.present().unwrap();
} }
_ => (),
let path = PathBuilder::from_rect(Rect::from_xywh(width as f32 - 30.0, 0.0, 30.0, 30.0).unwrap()); }
let mut paint = Paint::default();
paint.set_color_rgba8(220, 80, 80, 150);
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
let mut path = PathBuilder::new();
path.move_to(width as f32 - 22.5, 2.5);
path.line_to(width as f32 - 2.5, 27.5);
path.line_to(width as f32 - 5.0, 27.5);
path.line_to(width as f32 - 25.0, 2.5);
path.line_to(width as f32 - 22.5, 2.5);
let path = path.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(220, 220, 220, 255);
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
let mut path = PathBuilder::new();
path.move_to(width as f32 - 5.0, 2.5);
path.line_to(width as f32 - 25.0, 27.5);
path.line_to(width as f32 - 22.5, 27.5);
path.line_to(width as f32 - 2.5, 2.5);
path.line_to(width as f32 - 5.0, 2.5);
let path = path.finish().unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::EvenOdd,
Transform::identity(),
None,
);
let mut buffer = win.surface.buffer_mut().unwrap();
for index in 0..(width * height) as usize {
buffer[index] = pixmap.data()[index * 4 + 2] as u32
| (pixmap.data()[index * 4 + 1] as u32) << 8
| (pixmap.data()[index * 4] as u32) << 16;
}
buffer.present().unwrap();
},
_ => (),
} }
}, _ => (),
_ => (), }
} })
}).unwrap(); .unwrap();
} }
#[derive(Debug)] #[derive(Debug)]
struct MyUserEvent { struct MyUserEvent {
window_builder: WindowBuilder, window_builder: WindowBuilder,
clipboard: ClipboardContent clipboard: ClipboardContent,
} }
fn popup_clipboard(content: ClipboardContent) -> MyUserEvent { fn popup_clipboard(content: ClipboardContent) -> MyUserEvent {
@ -494,19 +570,22 @@ fn popup_clipboard(content: ClipboardContent) -> MyUserEvent {
.with_enabled_buttons(WindowButtons::empty()) .with_enabled_buttons(WindowButtons::empty())
.with_decorations(false) .with_decorations(false)
.with_window_level(WindowLevel::AlwaysOnTop) .with_window_level(WindowLevel::AlwaysOnTop)
.with_title("FONotes - ".to_owned() + (match content { .with_title(
ClipboardContent::Image(_) => "Image", "FONotes - ".to_owned()
ClipboardContent::Text(_) => "Text", + (match content {
_ => "???" ClipboardContent::Image(_) => "Image",
})) ClipboardContent::Text(_) => "Text",
_ => "???",
}),
)
.with_inner_size(match &content { .with_inner_size(match &content {
ClipboardContent::Image(i) => LogicalSize::new(i.width as f32, i.height as f32), ClipboardContent::Image(i) => LogicalSize::new(i.width as f32, i.height as f32),
_ => LogicalSize::new(250.0, 300.0) _ => LogicalSize::new(250.0, 300.0),
}) })
.with_resizable(true) .with_resizable(true)
.with_visible(true) .with_visible(true)
.with_min_inner_size(LogicalSize::new(50.0, 50.0)), .with_min_inner_size(LogicalSize::new(50.0, 50.0)),
clipboard: content clipboard: content,
} }
} }
@ -526,17 +605,21 @@ fn main() {
pressed.push(key); pressed.push(key);
} }
if key == Key::KeyN && if key == Key::KeyN
pressed.contains(&Key::ControlLeft) && && pressed.contains(&Key::ControlLeft)
pressed.contains(&Key::Alt) { && pressed.contains(&Key::Alt)
event_loop_proxy.send_event(popup_clipboard(get_clipboard(&mut clipboard))).unwrap(); {
event_loop_proxy
.send_event(popup_clipboard(get_clipboard(&mut clipboard)))
.unwrap();
} }
} else if let EventType::KeyRelease(key) = event.event_type { } else if let EventType::KeyRelease(key) = event.event_type {
if pressed.contains(&key) { if pressed.contains(&key) {
pressed.remove(pressed.iter().position(|x| *x == key).unwrap()); pressed.remove(pressed.iter().position(|x| *x == key).unwrap());
} }
} }
}).unwrap(); })
.unwrap();
}); });
run_event_loop(event_loop, windows); run_event_loop(event_loop, windows);