Skip to content

Commit

Permalink
toolkit库引入基本的事件处理框架 资源管理器初步实现交互 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
2447742618 authored Apr 14, 2024
1 parent b026285 commit 8a41b76
Show file tree
Hide file tree
Showing 12 changed files with 363 additions and 60 deletions.
3 changes: 2 additions & 1 deletion starry_applications/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ authors = [ "2447742618 <2447742618@qq.com>" ]
[dependencies]
starry_client = {path = "../starry_client" }
starry_toolkit = {path = "../starry_toolkit" }
starry_server = {path = "../starry_server" }
starry_server = {path = "../starry_server" }
termios = "0.3"
17 changes: 14 additions & 3 deletions starry_applications/src/asset_manager/code/asset_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use starry_toolkit::{
base::{rect::Rect, vector2::Vector2},
traits::text::Text,
widgets::{
align_rect,
image::Image,
label::{Label, LabelOverflowType},
widget_add_child, PivotType, Widget,
Expand All @@ -28,7 +29,7 @@ pub struct AssetItem {
children: RefCell<Vec<Arc<dyn Widget>>>,
/// 缓存值
cache_focused: Cell<bool>,
_file_path: RefCell<String>,
pub file_path: RefCell<String>,
}

impl AssetItem {
Expand All @@ -43,7 +44,7 @@ impl AssetItem {
parent: RefCell::new(None),
children: RefCell::new(Vec::new()),
cache_focused: Cell::new(false),
_file_path: RefCell::new(String::from_str(file_name).unwrap()),
file_path: RefCell::new(String::from_str(file_name).unwrap()),
});

// 背景Image
Expand Down Expand Up @@ -109,12 +110,22 @@ impl Widget for AssetItem {
children[0] = Image::from_color(
Self::ITEM_WIDTH,
Self::ITEM_HEIGHT,
Color::rgba(0, 255, 255, 128),
Color::rgba(0, 255, 255, 64),
);
} else {
children[0] =
Image::from_color(Self::ITEM_WIDTH, Self::ITEM_HEIGHT, Color::rgba(0, 0, 0, 0));
}

// TODO
// children[0].set_pivot_type(PivotType::Center);

children[0].rect().set(align_rect(
children[0].rect().get(),
self.rect.get(),
PivotType::Center,
Vector2::new(0, 0),
))
}

for child in self.children.borrow().iter() {
Expand Down
152 changes: 120 additions & 32 deletions starry_applications/src/asset_manager/code/mod.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,164 @@
use self::asset_item::AssetItem;
use crate::starry_toolkit::traits::focus::Focus;
use starry_client::base::color::Color;
use starry_server::base::image::Image as ImageResource;
use starry_server::core::{SCREEN_HEIGHT, SCREEN_WIDTH};
use starry_toolkit::{
base::{panel::Panel, rect::Rect},
layout::grid::{Grid, GridArrangeType},
traits::enter::Enter,
widgets::{image::Image, widget_add_child},
};
use std::{cell::RefCell, fs, sync::Arc};

use self::asset_item::AssetItem;
use crate::starry_server::base::image::Image as ImageResource;
use std::{collections::BTreeMap, fs, sync::Arc};

pub mod asset_item;

const DESKTOP_BG_PATH: &[u8] = include_bytes!("../resource/desktop_bg.png");

pub struct AssetViewer {
pub struct AssetManager {
cur_path: String,
asset_grid: RefCell<Arc<Grid>>,
asset_grid: Arc<Grid>,
items: BTreeMap<(usize, usize), Arc<AssetItem>>,
panel: Panel,
}

impl AssetViewer {
impl AssetManager {
pub fn new() -> Self {
AssetViewer {
AssetManager {
cur_path: String::from("/"),
asset_grid: RefCell::new(Grid::new()),
asset_grid: Grid::new(),
items: BTreeMap::new(),
panel: Panel::new(
Rect::new(0, 0, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32),
"Title",
Color::rgb(0, 0, 0),
),
}
}

pub fn init(&self) {
let grid = self.asset_grid.borrow();
pub fn init(&mut self) {
let grid = self.asset_grid.clone();
grid.set_upper_limit(5);
grid.set_space(20, 20);
grid.set_arrange_type(GridArrangeType::Horizontal);

let self_ptr = self as *mut AssetManager;
grid.set_enter_callback(move |grid, char, redraw| {
if char == ' ' {
let asset_manager: &mut AssetManager = unsafe { &mut *self_ptr };

if let Some(item) = asset_manager.items.get(&grid.focused_id.get().unwrap()) {
asset_manager.cur_path.push_str(&item.file_path.borrow());
asset_manager.cur_path.push_str(&"/");
asset_manager.refresh();
}

return;
}

let row_offset: i32 = match char {
'a' => 0,
'A' => 0,
'd' => 0,
'D' => 0,
'w' => -1,
'W' => -1,
's' => 1,
'S' => 1,
_ => 0,
};

let col_offset: i32 = match char {
'a' => -1,
'A' => -1,
'd' => 1,
'D' => 1,
'w' => 0,
'W' => 0,
's' => 0,
'S' => 0,
_ => 0,
};

if row_offset == 0 && col_offset == 0 {
return;
}
let mut nxt_row = grid.focused_id.get().unwrap().0 as i32 + row_offset;
let mut nxt_col = grid.focused_id.get().unwrap().1 as i32 + col_offset;
loop {
if nxt_row < 0
|| nxt_row >= grid.max_row.get() as i32
|| nxt_col < 0
|| nxt_col >= grid.max_column.get() as i32
{
return;
}

if grid
.elements
.borrow()
.contains_key(&(nxt_row as usize, nxt_col as usize))
{
break;
}

nxt_row += row_offset;
nxt_col += col_offset;
}

grid.focus(
grid.elements
.borrow()
.get(&(nxt_row as usize, nxt_col as usize))
.unwrap(),
);
grid.focused_id
.set(Some((nxt_row as usize, nxt_col as usize)));

*redraw = true;
});

self.panel.add_child(&Image::from_image(
ImageResource::from_path(DESKTOP_BG_PATH).unwrap(),
));

self.panel.add_child(&(self.asset_grid));
}

pub fn refresh(&self) {
pub fn refresh(&mut self) {
self.items.clear();
self.asset_grid.clear();

// 读取目录中的文件列表
if let Ok(entries) = fs::read_dir(&self.cur_path) {
for entry in entries {
if let Ok(item) = entry {
let item = AssetItem::new(
let asset_item = AssetItem::new(
item.file_name().to_str().unwrap(),
item.metadata().unwrap().is_dir(),
);
self.asset_grid.borrow_mut().add(&item);
widget_add_child(self.asset_grid.borrow().clone(), item);
let (row, col) = self.asset_grid.add(&asset_item);
self.items.insert((row, col), asset_item.clone());
widget_add_child(self.asset_grid.clone(), asset_item);
}
}
} else {
println!(
"[Error] AssetManager failed to read dir {:?}",
self.cur_path
);
}

// TODO 代码整理
let grid = self.asset_grid.borrow_mut();
let elements = grid.elements.borrow();
if let Some(widget) = elements.get(&(0, 0)) {
let grid = self.asset_grid.clone();
if let Some(widget) = grid.elements.borrow().get(&(0, 0)) {
grid.focused_id.set(Some((0, 0)));
grid.focus(widget);
}
}

pub fn draw(&self) {
let panel = Panel::new(
Rect::new(0, 0, SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32),
"Title",
Color::rgb(0, 0, 0),
);

panel.add_child(&Image::from_image(
ImageResource::from_path(DESKTOP_BG_PATH).unwrap(),
));
self.panel.draw();
}

panel.add_child(&(self.asset_grid.borrow()));
panel.draw();
pub fn exec(&mut self) {
self.panel.exec();
}
}
35 changes: 35 additions & 0 deletions starry_applications/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
extern crate starry_client;
extern crate starry_server;
extern crate starry_toolkit;
extern crate termios;

pub mod asset_manager;

use std::io;
use std::os::unix::io::AsRawFd;
use termios::{tcsetattr, Termios};

// TODO
#[allow(dead_code)]
pub fn set_terminal() -> io::Result<()> {
let stdin = io::stdin().as_raw_fd();
let stored_settings = Termios::from_fd(stdin)?;

let mut new_settings = stored_settings.clone();
new_settings.c_lflag &= !(termios::ICANON) & !(termios::ECHO);
new_settings.c_cc[termios::VMIN] = 1;
new_settings.c_cc[termios::VTIME] = 0;

tcsetattr(stdin, termios::TCSANOW, &new_settings)?;

Ok(())
}

// TODO
#[allow(dead_code)]
pub fn reset_terminal() -> io::Result<()> {
let stdin = io::stdin().as_raw_fd();
let stored_settings = Termios::from_fd(stdin)?;

let mut new_settings = stored_settings.clone();
new_settings.c_lflag &= (termios::ICANON) & (termios::ECHO);

tcsetattr(stdin, termios::TCSANOW, &new_settings)?;

Ok(())
}
11 changes: 6 additions & 5 deletions starry_applications/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use starry_apps::asset_manager::code::AssetViewer;
use starry_apps::asset_manager::code::AssetManager;

fn main() {
let viewer = AssetViewer::new();
// set_terminal();

// TODO
let mut viewer = AssetManager::new();
viewer.init();
viewer.refresh();
viewer.draw();

loop {}
viewer.exec();
}
35 changes: 35 additions & 0 deletions starry_toolkit/src/base/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use super::vector2::Vector2;

#[derive(Copy, Clone, Debug)]
pub enum Event {
Init,

Mouse {
point: Vector2,
left_button: bool,
middle_button: bool,
right_button: bool,
},

KeyPressed {
character: Option<char>,
scancode: u8,
},

KeyReleased {
character: Option<char>,
scancode: u8,
},

Scroll {
x: i32,
y: i32,
},

Resize {
width: u32,
height: u32,
},

Unknown,
}
1 change: 1 addition & 0 deletions starry_toolkit/src/base/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod event;
pub mod panel;
pub mod rect;
pub mod vector2;
Loading

0 comments on commit 8a41b76

Please sign in to comment.