Browse Source

添加fmt-check并修复一些问题 (#6)

yuyi2439 11 months ago
parent
commit
1c11e7de96

+ 7 - 1
Makefile

@@ -1,3 +1,9 @@
 fmt:
 	@ $(MAKE) -C starry_client fmt
-	@ $(MAKE) -C starry_server fmt
+	@ $(MAKE) -C starry_server fmt
+	@ $(MAKE) -C starry_toolkit fmt
+
+fmt-check:
+	@ $(MAKE) -C starry_client fmt-check
+	@ $(MAKE) -C starry_server fmt-check
+	@ $(MAKE) -C starry_toolkit fmt-check

+ 6 - 8
starry_client/src/base/renderer.rs

@@ -5,7 +5,7 @@ use super::{
     graphicspath::{GraphicsPath, PointType},
 };
 
-static FONT_ASSET : &[u8] = include_bytes!("../font/unifont.font");
+static FONT_ASSET: &[u8] = include_bytes!("../font/unifont.font");
 
 /// 渲染模式: 混合/覆盖
 #[derive(Clone, Copy, Debug)]
@@ -144,7 +144,7 @@ pub trait Renderer {
             }
         }
     }
-    
+
     /// TODO 注释补充
     fn circle(&mut self, x0: i32, y0: i32, radius: i32, color: Color) {
         let mut x = radius.abs();
@@ -205,7 +205,7 @@ pub trait Renderer {
             self.rect(x0 - x, y0 - y, x as u32 * 2 + 1, 1, color);
         }
     }
-    
+
     /// # 函数功能
     /// 绘制指定颜色的一条线段
     ///
@@ -387,7 +387,7 @@ pub trait Renderer {
 
     /// # 函数功能
     /// 在指定位置绘制字符
-    /// 
+    ///
     /// ## 参数
     /// - x: x坐标
     /// - y: y坐标
@@ -414,7 +414,7 @@ pub trait Renderer {
 
     /// # 函数功能
     /// 在指定位置绘制一幅图像至帧缓冲区
-    /// 
+    ///
     /// ## 参数
     /// - start_x: 起始x坐标(左上角)
     /// - start_y: 起始y坐标(左上角)
@@ -427,11 +427,10 @@ pub trait Renderer {
             RenderMode::Overwrite => self.image_opaque(start_x, start_y, w, h, data),
         }
     }
-    
 
     /// # 函数功能
     /// 从指定行开始绘制一幅图像至帧缓冲区
-    /// 
+    ///
     /// ## 参数
     /// - start: 起始行数
     /// - image_data: 图像帧缓冲数据
@@ -532,5 +531,4 @@ pub trait Renderer {
             }
         }
     }
-    
 }

+ 6 - 4
starry_client/src/window.rs

@@ -67,10 +67,12 @@ impl Renderer for Window {
         for y in 0..self.height() as i32 {
             for x in 0..self.width() as i32 {
                 let pixel = self.get_pixel(x, y);
-                let offset =  (((y + self.y()) * SCREEN_WIDTH as i32) + x + self.x()) * 4;
+                let offset = (((y + self.y()) * SCREEN_WIDTH as i32) + x + self.x()) * 4;
                 // 写缓冲区
-                fb.seek(SeekFrom::Start(offset as u64)).expect("Unable to seek framebuffer");
-                fb.write_all(&pixel.to_bgra_bytes()).expect("Unable to write framebuffer");
+                fb.seek(SeekFrom::Start(offset as u64))
+                    .expect("Unable to seek framebuffer");
+                fb.write_all(&pixel.to_bgra_bytes())
+                    .expect("Unable to write framebuffer");
             }
         }
         true
@@ -101,7 +103,6 @@ impl Window {
         // TODO: 与服务器通信
     }
 
-
     /// 返回窗口x坐标
     pub fn x(&self) -> i32 {
         self.x
@@ -115,6 +116,7 @@ impl Window {
     /// 返回窗口标题
     pub fn title(&self) -> String {
         self.title.clone()
+    }
 
     /// 改变窗口的位置
     pub fn set_pos(&mut self, x: i32, y: i32) {

+ 10 - 7
starry_server/src/base/image.rs

@@ -369,22 +369,25 @@ impl Image {
     pub fn resize(&self, w: u32, h: u32, resize_type: Type) -> Self {
         let mut dst_color = vec![Color { data: 0 }; w as usize * h as usize].into_boxed_slice();
 
-        let src = unsafe {
-            slice::from_raw_parts(self.data.as_ptr() as *const u8, self.data.len() * 4)
-        };
+        let src =
+            unsafe { slice::from_raw_parts(self.data.as_ptr() as *const u8, self.data.len() * 4) };
 
         let mut dst = unsafe {
             slice::from_raw_parts_mut(dst_color.as_mut_ptr() as *mut u8, dst_color.len() * 4)
         };
 
-        let mut resizer = resize::new(self.w as usize, self.h as usize,
-                                      w as usize, h as usize,
-                                      resize::Pixel::RGBA, resize_type);
+        let mut resizer = resize::new(
+            self.w as usize,
+            self.h as usize,
+            w as usize,
+            h as usize,
+            resize::Pixel::RGBA,
+            resize_type,
+        );
         resizer.resize(&src, &mut dst);
 
         Image::from_data(w as i32, h as i32, dst_color)
     }
-    
 }
 
 impl Renderer for Image {

+ 4 - 1
starry_server/src/core/mod.rs

@@ -52,7 +52,10 @@ impl StarryServer {
     pub fn new(config: Rc<Config>, displays: Vec<Display>) {
         let mut cursors = BTreeMap::new();
         cursors.insert(CursorKind::None, Image::new(0, 0));
-        cursors.insert(CursorKind::Normal, Image::from_path(CURSOR_NORMAL).unwrap_or(Image::new(10, 10)));        
+        cursors.insert(
+            CursorKind::Normal,
+            Image::from_path(CURSOR_NORMAL).unwrap_or(Image::new(10, 10)),
+        );
         // cursors.insert(CursorKind::BottomLeftCorner, Image::from_path_scale(&config.bottom_left_corner, scale).unwrap_or(Image::new(0, 0)));
         // cursors.insert(CursorKind::BottomRightCorner, Image::from_path_scale(&config.bottom_right_corner, scale).unwrap_or(Image::new(0, 0)));
         // cursors.insert(CursorKind::BottomSide, Image::from_path_scale(&config.bottom_side, scale).unwrap_or(Image::new(0, 0)));

+ 1 - 1
starry_server/src/lib.rs

@@ -3,4 +3,4 @@ pub mod config;
 pub mod core;
 
 extern crate bitflags;
-extern crate resize;
+extern crate resize;

+ 33 - 18
starry_toolkit/Makefile

@@ -1,41 +1,56 @@
-# The toolchain we use.
-# You can get it by running DragonOS' `tools/bootstrap.sh`
-TOOLCHAIN="+nightly-2023-08-15-x86_64-unknown-linux_dragonos-gnu"
-RUSTFLAGS+="-C target-feature=+crt-static -C link-arg=-no-pie"
+TOOLCHAIN="+nightly-2023-08-15-x86_64-unknown-linux-gnu"
+RUSTFLAGS+=""
 
+ifdef DADK_CURRENT_BUILD_DIR
 # 如果是在dadk中编译,那么安装到dadk的安装目录中
-INSTALL_DIR?=$(DADK_CURRENT_BUILD_DIR)
+	INSTALL_DIR = $(DADK_CURRENT_BUILD_DIR)
+else
 # 如果是在本地编译,那么安装到当前目录下的install目录中
-INSTALL_DIR?=./install
-
+	INSTALL_DIR = ./install
+endif
+
+ifeq ($(ARCH), x86_64)
+	export RUST_TARGET=x86_64-unknown-linux-musl
+else ifeq ($(ARCH), riscv64)
+	export RUST_TARGET=riscv64gc-unknown-linux-gnu
+else 
+# 默认为x86_86,用于本地编译
+	export RUST_TARGET=x86_64-unknown-linux-musl
+endif
 
 run:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET)
 
 build:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET)
 
 clean:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET)
 
 test:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET)
 
 doc:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) doc
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) doc --target $(RUST_TARGET)
+
+fmt:
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt
+
+fmt-check:
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) fmt --check
 
 run-release:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --release
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) run --target $(RUST_TARGET) --release
 
 build-release:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --release
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) build --target $(RUST_TARGET) --release
 
 clean-release:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --release
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) clean --target $(RUST_TARGET) --release
 
 test-release:
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --release
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) test --target $(RUST_TARGET) --release
 
 .PHONY: install
-install: 
-	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --path . --no-track --root $(INSTALL_DIR) --force
+install:
+	RUSTFLAGS=$(RUSTFLAGS) cargo $(TOOLCHAIN) install --target $(RUST_TARGET) --path . --no-track --root $(INSTALL_DIR) --force

+ 18 - 15
starry_toolkit/src/base/panel.rs

@@ -1,7 +1,15 @@
-
-use std::{cell::{Cell, RefCell}, sync::Arc};
-
-use starry_client::{base::{color::Color, renderer::{RenderMode, Renderer}}, window::Window};
+use std::{
+    cell::{Cell, RefCell},
+    sync::Arc,
+};
+
+use starry_client::{
+    base::{
+        color::Color,
+        renderer::{RenderMode, Renderer},
+    },
+    window::Window,
+};
 
 use crate::widgets::Widget;
 
@@ -10,14 +18,12 @@ use super::rect::Rect;
 /// 面板渲染器
 pub struct PanelRenderer<'a> {
     /// 客户端窗口
-    window: & 'a mut Window,
+    window: &'a mut Window,
 }
 
 impl<'a> PanelRenderer<'a> {
-    pub fn new(window: &'a mut Window) -> Self{
-        PanelRenderer {
-            window,
-        }
+    pub fn new(window: &'a mut Window) -> Self {
+        PanelRenderer { window }
     }
 }
 
@@ -46,7 +52,7 @@ impl<'a> Renderer for PanelRenderer<'a> {
         &self.window.mode()
     }
 
-    // TODO 
+    // TODO
     // fn char(&mut self, x: i32, y: i32, c: char, color: Color) {
     // }
 }
@@ -70,9 +76,7 @@ pub struct Panel {
 
 impl Panel {
     pub fn new(rect: Rect, title: &str) -> Self {
-        Panel::from_window(
-            Window::new(rect.x, rect.y, rect.width, rect.height, title)
-        )
+        Panel::from_window(Window::new(rect.x, rect.y, rect.width, rect.height, title))
     }
 
     pub fn from_window(window: Window) -> Self {
@@ -172,5 +176,4 @@ impl Panel {
             self.draw_widget(renderer, child);
         }
     }
-
-}
+}

+ 1 - 1
starry_toolkit/src/base/point.rs

@@ -35,4 +35,4 @@ impl Sub for Point {
             y: self.y - other.y,
         }
     }
-}
+}

+ 1 - 1
starry_toolkit/src/traits/mod.rs

@@ -1,2 +1,2 @@
 pub mod place;
-pub mod text;
+pub mod text;

+ 4 - 1
starry_toolkit/src/widgets/image.rs

@@ -5,7 +5,10 @@ use std::{
 
 use starry_client::base::{color::Color, renderer::Renderer};
 
-use crate::{base::{point::Point, rect::Rect}, traits::place::Place};
+use crate::{
+    base::{point::Point, rect::Rect},
+    traits::place::Place,
+};
 
 use super::{HorizontalPlacement, VerticalPlacement, Widget};
 

+ 12 - 4
starry_toolkit/src/widgets/label.rs

@@ -5,7 +5,10 @@ use std::{
 
 use starry_client::base::{color::Color, renderer::Renderer};
 
-use crate::{base::{point::Point, rect::Rect}, traits::{place::Place, text::Text}};
+use crate::{
+    base::{point::Point, rect::Rect},
+    traits::{place::Place, text::Text},
+};
 
 use super::{HorizontalPlacement, VerticalPlacement, Widget};
 
@@ -20,7 +23,7 @@ pub struct Label {
 }
 
 impl Label {
-        pub fn new() -> Arc<Self> {
+    pub fn new() -> Arc<Self> {
         Arc::new(Label {
             rect: Cell::new(Rect::default()),
             local_position: Cell::new(Point::new(0, 0)),
@@ -86,7 +89,12 @@ impl Widget for Label {
                 {
                     // 默认渲染白色字体
                     // TODO 应用主题(Theme)颜色
-                    renderer.char(current_rect.x, current_rect.y, char, Color::rgb(255, 255, 255));
+                    renderer.char(
+                        current_rect.x,
+                        current_rect.y,
+                        char,
+                        Color::rgb(255, 255, 255),
+                    );
                 }
                 current_rect.x += 8;
             }
@@ -109,4 +117,4 @@ impl Text for Label {
         self.adjust_size();
         self
     }
-}
+}