chore(fmt): add legacy stubs and strip trailing whitespace to unblock cargo fmt

This commit is contained in:
Selfhosting Dev
2025-09-17 07:43:07 +09:00
parent fcf8ce1f3c
commit adbb0201a9
385 changed files with 35622 additions and 15004 deletions

View File

@ -1,21 +1,18 @@
/*!
* WebCanvasBox - ブラウザCanvas完全制御Box
*
*
* WebAssembly環境でHTML5 Canvasの完全制御
* ピクセルの世界を制圧する革命的Box
*/
use crate::box_trait::{NyashBox, StringBox, BoolBox, BoxCore, BoxBase};
use crate::box_trait::{BoolBox, BoxBase, BoxCore, NyashBox, StringBox};
use std::any::Any;
#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;
#[cfg(target_arch = "wasm32")]
use web_sys::{
HtmlCanvasElement,
CanvasRenderingContext2d,
};
use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement};
// 🎨 Browser Canvas complete control Box
#[cfg(target_arch = "wasm32")]
@ -30,22 +27,22 @@ pub struct WebCanvasBox {
#[cfg(target_arch = "wasm32")]
impl WebCanvasBox {
pub fn new(canvas_id: String, width: u32, height: u32) -> Self {
let instance = Self {
let instance = Self {
base: BoxBase::new(),
canvas_id: canvas_id.clone(),
width,
height,
};
// キャンバス要素を初期化
if let Some(canvas) = instance.get_canvas_element() {
canvas.set_width(width);
canvas.set_height(height);
}
instance
}
/// Canvas要素を取得
fn get_canvas_element(&self) -> Option<HtmlCanvasElement> {
let window = web_sys::window()?;
@ -53,7 +50,7 @@ impl WebCanvasBox {
let element = document.get_element_by_id(&self.canvas_id)?;
element.dyn_into::<HtmlCanvasElement>().ok()
}
/// 2Dレンダリングコンテキストを取得
fn get_2d_context(&self) -> Option<CanvasRenderingContext2d> {
let canvas = self.get_canvas_element()?;
@ -62,35 +59,35 @@ impl WebCanvasBox {
.ok()?
.and_then(|ctx| ctx.dyn_into::<CanvasRenderingContext2d>().ok())
}
/// キャンバスをクリア
pub fn clear(&self) {
if let Some(ctx) = self.get_2d_context() {
ctx.clear_rect(0.0, 0.0, self.width as f64, self.height as f64);
}
}
/// 塗りつぶし色を設定
pub fn set_fill_style(&self, color: &str) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_fill_style(&wasm_bindgen::JsValue::from_str(color));
}
}
/// 線の色を設定
pub fn set_stroke_style(&self, color: &str) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(color));
}
}
/// 線の太さを設定
pub fn set_line_width(&self, width: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_line_width(width);
}
}
/// 塗りつぶし矩形を描画
pub fn fill_rect(&self, x: f64, y: f64, width: f64, height: f64, color: &str) {
if let Some(ctx) = self.get_2d_context() {
@ -98,37 +95,47 @@ impl WebCanvasBox {
ctx.fill_rect(x, y, width, height);
}
}
/// 枠線矩形を描画
pub fn stroke_rect(&self, x: f64, y: f64, width: f64, height: f64, color: &str, line_width: f64) {
pub fn stroke_rect(
&self,
x: f64,
y: f64,
width: f64,
height: f64,
color: &str,
line_width: f64,
) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(color));
ctx.set_line_width(line_width);
ctx.stroke_rect(x, y, width, height);
}
}
/// 塗りつぶし円を描画
pub fn fill_circle(&self, x: f64, y: f64, radius: f64, color: &str) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_fill_style(&wasm_bindgen::JsValue::from_str(color));
ctx.begin_path();
ctx.arc(x, y, radius, 0.0, 2.0 * std::f64::consts::PI).unwrap_or_default();
ctx.arc(x, y, radius, 0.0, 2.0 * std::f64::consts::PI)
.unwrap_or_default();
ctx.fill();
}
}
/// 枠線円を描画
pub fn stroke_circle(&self, x: f64, y: f64, radius: f64, color: &str, line_width: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(color));
ctx.set_line_width(line_width);
ctx.begin_path();
ctx.arc(x, y, radius, 0.0, 2.0 * std::f64::consts::PI).unwrap_or_default();
ctx.arc(x, y, radius, 0.0, 2.0 * std::f64::consts::PI)
.unwrap_or_default();
ctx.stroke();
}
}
/// 直線を描画
pub fn draw_line(&self, x1: f64, y1: f64, x2: f64, y2: f64, color: &str, line_width: f64) {
if let Some(ctx) = self.get_2d_context() {
@ -140,7 +147,7 @@ impl WebCanvasBox {
ctx.stroke();
}
}
/// テキストを描画(塗りつぶし)
pub fn fill_text(&self, text: &str, x: f64, y: f64, font: &str, color: &str) {
if let Some(ctx) = self.get_2d_context() {
@ -149,9 +156,17 @@ impl WebCanvasBox {
ctx.fill_text(text, x, y).unwrap_or_default();
}
}
/// テキストを描画(枠線)
pub fn stroke_text(&self, text: &str, x: f64, y: f64, font: &str, color: &str, line_width: f64) {
pub fn stroke_text(
&self,
text: &str,
x: f64,
y: f64,
font: &str,
color: &str,
line_width: f64,
) {
if let Some(ctx) = self.get_2d_context() {
ctx.set_font(font);
ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(color));
@ -159,35 +174,35 @@ impl WebCanvasBox {
ctx.stroke_text(text, x, y).unwrap_or_default();
}
}
/// パス描画開始
pub fn begin_path(&self) {
if let Some(ctx) = self.get_2d_context() {
ctx.begin_path();
}
}
/// パスを指定位置に移動
pub fn move_to(&self, x: f64, y: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.move_to(x, y);
}
}
/// パスに直線を追加
pub fn line_to(&self, x: f64, y: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.line_to(x, y);
}
}
/// パスを閉じる
pub fn close_path(&self) {
if let Some(ctx) = self.get_2d_context() {
ctx.close_path();
}
}
/// パスを塗りつぶし
pub fn fill(&self, color: &str) {
if let Some(ctx) = self.get_2d_context() {
@ -195,7 +210,7 @@ impl WebCanvasBox {
ctx.fill();
}
}
/// パスを枠線描画
pub fn stroke(&self, color: &str, line_width: f64) {
if let Some(ctx) = self.get_2d_context() {
@ -204,56 +219,56 @@ impl WebCanvasBox {
ctx.stroke();
}
}
/// 現在の描画状態を保存
pub fn save(&self) {
if let Some(ctx) = self.get_2d_context() {
ctx.save();
}
}
/// 描画状態を復元
pub fn restore(&self) {
if let Some(ctx) = self.get_2d_context() {
ctx.restore();
}
}
/// 座標系を回転
pub fn rotate(&self, angle: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.rotate(angle).unwrap_or_default();
}
}
/// 座標系をスケール
pub fn scale(&self, x: f64, y: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.scale(x, y).unwrap_or_default();
}
}
/// 座標系を平行移動
pub fn translate(&self, x: f64, y: f64) {
if let Some(ctx) = self.get_2d_context() {
ctx.translate(x, y).unwrap_or_default();
}
}
/// キャンバスのサイズを取得
pub fn get_width(&self) -> u32 {
self.width
}
pub fn get_height(&self) -> u32 {
self.height
}
/// キャンバスのサイズを変更
pub fn resize(&mut self, width: u32, height: u32) {
self.width = width;
self.height = height;
if let Some(canvas) = self.get_canvas_element() {
canvas.set_width(width);
canvas.set_height(height);
@ -266,19 +281,23 @@ impl BoxCore for WebCanvasBox {
fn box_id(&self) -> u64 {
self.base.id
}
fn parent_type_id(&self) -> Option<std::any::TypeId> {
self.base.parent_type_id
}
fn fmt_box(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "WebCanvasBox({}, {}x{})", self.canvas_id, self.width, self.height)
write!(
f,
"WebCanvasBox({}, {}x{})",
self.canvas_id, self.width, self.height
)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
@ -289,7 +308,7 @@ impl NyashBox for WebCanvasBox {
fn clone_box(&self) -> Box<dyn NyashBox> {
Box::new(self.clone())
}
/// 仮実装: clone_boxと同じ後で修正
fn share_box(&self) -> Box<dyn NyashBox> {
self.clone_box()
@ -297,18 +316,14 @@ impl NyashBox for WebCanvasBox {
fn to_string_box(&self) -> StringBox {
StringBox::new(format!(
"WebCanvasBox({}, {}x{})",
self.canvas_id,
self.width,
self.height
"WebCanvasBox({}, {}x{})",
self.canvas_id, self.width, self.height
))
}
fn type_name(&self) -> &'static str {
"WebCanvasBox"
}
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
if let Some(other_canvas) = other.as_any().downcast_ref::<WebCanvasBox>() {
@ -324,4 +339,4 @@ impl std::fmt::Display for WebCanvasBox {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.fmt_box(f)
}
}
}