🎉 initial commit: Nyash Programming Language完成版
🚀 主要機能: • Everything is Box哲学による革新的アーキテクチャ • WebAssemblyブラウザー対応プレイグラウンド • アーティスト協同制作デモ - 複数Boxインスタンス実証 • 視覚的デバッグシステム - DebugBox完全統合 • static box Mainパターン - メモリ安全設計 ⚡ 言語機能: • NOT/AND/OR/除算演算子完全実装 • ジェネリクス/テンプレートシステム • 非同期処理(nowait/await) • try/catchエラーハンドリング • Canvas統合グラフィックス 🎨 ブラウザー体験: • 9種類のインタラクティブデモ • リアルタイムコード実行 • WebCanvas/WebConsole/WebDisplay • モバイル対応完了 🤖 Built with Claude Code collaboration Ready for public release!
This commit is contained in:
24
src/boxes/web/mod.rs
Normal file
24
src/boxes/web/mod.rs
Normal file
@ -0,0 +1,24 @@
|
||||
/*!
|
||||
* Web Boxes Module - ブラウザ専用Box群
|
||||
*
|
||||
* WebAssembly環境専用のBox群を管理
|
||||
* HTML5 APIs、DOM操作、Canvas描画等をNyashから利用可能にする
|
||||
*/
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub mod web_display_box;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub mod web_console_box;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub mod web_canvas_box;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use web_display_box::WebDisplayBox;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use web_console_box::WebConsoleBox;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
pub use web_canvas_box::WebCanvasBox;
|
||||
304
src/boxes/web/web_canvas_box.rs
Normal file
304
src/boxes/web/web_canvas_box.rs
Normal file
@ -0,0 +1,304 @@
|
||||
/*!
|
||||
* WebCanvasBox - ブラウザCanvas完全制御Box
|
||||
*
|
||||
* WebAssembly環境でHTML5 Canvasの完全制御
|
||||
* ピクセルの世界を制圧する革命的Box!
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use web_sys::{
|
||||
HtmlCanvasElement,
|
||||
CanvasRenderingContext2d,
|
||||
};
|
||||
|
||||
// 🎨 Browser Canvas complete control Box
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WebCanvasBox {
|
||||
id: u64,
|
||||
canvas_id: String,
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl WebCanvasBox {
|
||||
pub fn new(canvas_id: String, width: u32, height: u32) -> Self {
|
||||
static mut COUNTER: u64 = 0;
|
||||
let id = unsafe {
|
||||
COUNTER += 1;
|
||||
COUNTER
|
||||
};
|
||||
|
||||
let instance = Self {
|
||||
id,
|
||||
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()?;
|
||||
let document = window.document()?;
|
||||
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()?;
|
||||
canvas
|
||||
.get_context("2d")
|
||||
.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() {
|
||||
ctx.set_fill_style(&wasm_bindgen::JsValue::from_str(color));
|
||||
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) {
|
||||
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.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.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() {
|
||||
ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(color));
|
||||
ctx.set_line_width(line_width);
|
||||
ctx.begin_path();
|
||||
ctx.move_to(x1, y1);
|
||||
ctx.line_to(x2, y2);
|
||||
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() {
|
||||
ctx.set_font(font);
|
||||
ctx.set_fill_style(&wasm_bindgen::JsValue::from_str(color));
|
||||
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) {
|
||||
if let Some(ctx) = self.get_2d_context() {
|
||||
ctx.set_font(font);
|
||||
ctx.set_stroke_style(&wasm_bindgen::JsValue::from_str(color));
|
||||
ctx.set_line_width(line_width);
|
||||
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() {
|
||||
ctx.set_fill_style(&wasm_bindgen::JsValue::from_str(color));
|
||||
ctx.fill();
|
||||
}
|
||||
}
|
||||
|
||||
/// パスを枠線描画
|
||||
pub fn stroke(&self, 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();
|
||||
}
|
||||
}
|
||||
|
||||
/// 現在の描画状態を保存
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl NyashBox for WebCanvasBox {
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!(
|
||||
"WebCanvasBox({}, {}x{})",
|
||||
self.canvas_id,
|
||||
self.width,
|
||||
self.height
|
||||
))
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"WebCanvasBox"
|
||||
}
|
||||
|
||||
fn box_id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_canvas) = other.as_any().downcast_ref::<WebCanvasBox>() {
|
||||
BoolBox::new(self.id == other_canvas.id)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
175
src/boxes/web/web_console_box.rs
Normal file
175
src/boxes/web/web_console_box.rs
Normal file
@ -0,0 +1,175 @@
|
||||
/*!
|
||||
* WebConsoleBox - ブラウザHTML要素コンソール出力Box
|
||||
*
|
||||
* WebAssembly環境でHTML要素へのコンソール風出力
|
||||
* F12コンソールの代わりに指定要素に出力
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use web_sys::{Element, HtmlElement};
|
||||
|
||||
// 🌐 Browser HTML element console output Box
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WebConsoleBox {
|
||||
id: u64,
|
||||
target_element_id: String,
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl WebConsoleBox {
|
||||
pub fn new(element_id: String) -> Self {
|
||||
static mut COUNTER: u64 = 0;
|
||||
let id = unsafe {
|
||||
COUNTER += 1;
|
||||
COUNTER
|
||||
};
|
||||
Self {
|
||||
id,
|
||||
target_element_id: element_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// 指定した要素IDのHTML要素を取得
|
||||
fn get_target_element(&self) -> Option<Element> {
|
||||
let window = web_sys::window()?;
|
||||
let document = window.document()?;
|
||||
document.get_element_by_id(&self.target_element_id)
|
||||
}
|
||||
|
||||
/// コンソール出力を追加(改行付き)
|
||||
fn append_console_line(&self, message: &str, level: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let timestamp = js_sys::Date::new_0().to_iso_string().as_string().unwrap_or_default();
|
||||
let time_part = timestamp.split('T').nth(1).unwrap_or("00:00:00").split('.').nth(0).unwrap_or("00:00:00");
|
||||
|
||||
let (level_prefix, color) = match level {
|
||||
"log" => ("📝", "white"),
|
||||
"warn" => ("⚠️", "yellow"),
|
||||
"error" => ("❌", "red"),
|
||||
"info" => ("ℹ️", "cyan"),
|
||||
"debug" => ("🔍", "gray"),
|
||||
_ => ("📝", "white"),
|
||||
};
|
||||
|
||||
let formatted_line = format!(
|
||||
"<span style='color: {}'>[{}] {} {}</span><br>",
|
||||
color,
|
||||
time_part,
|
||||
level_prefix,
|
||||
message
|
||||
);
|
||||
|
||||
let current_content = element.inner_html();
|
||||
let new_content = format!("{}{}", current_content, formatted_line);
|
||||
element.set_inner_html(&new_content);
|
||||
|
||||
// 自動スクロール
|
||||
if let Some(html_element) = element.dyn_ref::<HtmlElement>() {
|
||||
html_element.set_scroll_top(html_element.scroll_height());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// ログメッセージを出力
|
||||
pub fn log(&self, message: &str) {
|
||||
self.append_console_line(message, "log");
|
||||
}
|
||||
|
||||
/// 警告メッセージを出力
|
||||
pub fn warn(&self, message: &str) {
|
||||
self.append_console_line(message, "warn");
|
||||
}
|
||||
|
||||
/// エラーメッセージを出力
|
||||
pub fn error(&self, message: &str) {
|
||||
self.append_console_line(message, "error");
|
||||
}
|
||||
|
||||
/// 情報メッセージを出力
|
||||
pub fn info(&self, message: &str) {
|
||||
self.append_console_line(message, "info");
|
||||
}
|
||||
|
||||
/// デバッグメッセージを出力
|
||||
pub fn debug(&self, message: &str) {
|
||||
self.append_console_line(message, "debug");
|
||||
}
|
||||
|
||||
/// コンソールをクリア
|
||||
pub fn clear(&self) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
element.set_inner_html("");
|
||||
}
|
||||
}
|
||||
|
||||
/// 区切り線を追加
|
||||
pub fn separator(&self) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let current_content = element.inner_html();
|
||||
let separator_line = "<hr style='border: 1px solid #333; margin: 5px 0;'>";
|
||||
let new_content = format!("{}{}", current_content, separator_line);
|
||||
element.set_inner_html(&new_content);
|
||||
}
|
||||
}
|
||||
|
||||
/// グループ開始(見出し付き)
|
||||
pub fn group(&self, title: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let current_content = element.inner_html();
|
||||
let group_header = format!(
|
||||
"<div style='font-weight: bold; color: #4ecdc4; margin: 10px 0 5px 0;'>📂 {}</div><div style='margin-left: 20px; color: white;'>",
|
||||
title
|
||||
);
|
||||
let new_content = format!("{}{}", current_content, group_header);
|
||||
element.set_inner_html(&new_content);
|
||||
}
|
||||
}
|
||||
|
||||
/// グループ終了
|
||||
pub fn group_end(&self) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let current_content = element.inner_html();
|
||||
let group_footer = "</div>";
|
||||
let new_content = format!("{}{}", current_content, group_footer);
|
||||
element.set_inner_html(&new_content);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl NyashBox for WebConsoleBox {
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("WebConsoleBox({})", self.target_element_id))
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"WebConsoleBox"
|
||||
}
|
||||
|
||||
fn box_id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_console) = other.as_any().downcast_ref::<WebConsoleBox>() {
|
||||
BoolBox::new(self.id == other_console.id)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
169
src/boxes/web/web_display_box.rs
Normal file
169
src/boxes/web/web_display_box.rs
Normal file
@ -0,0 +1,169 @@
|
||||
/*!
|
||||
* WebDisplayBox - ブラウザHTML要素表示制御Box
|
||||
*
|
||||
* WebAssembly環境でHTML要素への直接出力・スタイル制御
|
||||
* プレイグラウンドの出力パネル等を完全制御
|
||||
*/
|
||||
|
||||
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||
use std::any::Any;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
use web_sys::{Element, HtmlElement};
|
||||
|
||||
// 🌐 Browser HTML element display control Box
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct WebDisplayBox {
|
||||
id: u64,
|
||||
target_element_id: String,
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl WebDisplayBox {
|
||||
pub fn new(element_id: String) -> Self {
|
||||
static mut COUNTER: u64 = 0;
|
||||
let id = unsafe {
|
||||
COUNTER += 1;
|
||||
COUNTER
|
||||
};
|
||||
Self {
|
||||
id,
|
||||
target_element_id: element_id,
|
||||
}
|
||||
}
|
||||
|
||||
/// 指定した要素IDのHTML要素を取得
|
||||
fn get_target_element(&self) -> Option<Element> {
|
||||
let window = web_sys::window()?;
|
||||
let document = window.document()?;
|
||||
document.get_element_by_id(&self.target_element_id)
|
||||
}
|
||||
|
||||
/// テキストを追加出力
|
||||
pub fn print(&self, message: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let current_content = element.inner_html();
|
||||
let new_content = if current_content.is_empty() {
|
||||
message.to_string()
|
||||
} else {
|
||||
format!("{}{}", current_content, message)
|
||||
};
|
||||
element.set_inner_html(&new_content);
|
||||
}
|
||||
}
|
||||
|
||||
/// テキストを改行付きで追加出力
|
||||
pub fn println(&self, message: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let current_content = element.inner_html();
|
||||
let new_content = if current_content.is_empty() {
|
||||
message.to_string()
|
||||
} else {
|
||||
format!("{}<br>{}", current_content, message)
|
||||
};
|
||||
element.set_inner_html(&new_content);
|
||||
}
|
||||
}
|
||||
|
||||
/// HTMLコンテンツを完全置換
|
||||
pub fn set_html(&self, html_content: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
element.set_inner_html(html_content);
|
||||
}
|
||||
}
|
||||
|
||||
/// HTMLコンテンツを追加
|
||||
pub fn append_html(&self, html_content: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let current_content = element.inner_html();
|
||||
let new_content = format!("{}{}", current_content, html_content);
|
||||
element.set_inner_html(&new_content);
|
||||
}
|
||||
}
|
||||
|
||||
/// CSSスタイルを設定
|
||||
pub fn set_css(&self, property: &str, value: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
if let Some(html_element) = element.dyn_ref::<HtmlElement>() {
|
||||
// HTMLElement の style プロパティへアクセス
|
||||
let _ = html_element.style().set_property(property, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// CSSクラスを追加
|
||||
pub fn add_class(&self, class_name: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let _ = element.class_list().add_1(class_name);
|
||||
}
|
||||
}
|
||||
|
||||
/// CSSクラスを削除
|
||||
pub fn remove_class(&self, class_name: &str) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
let _ = element.class_list().remove_1(class_name);
|
||||
}
|
||||
}
|
||||
|
||||
/// 内容をクリア
|
||||
pub fn clear(&self) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
element.set_inner_html("");
|
||||
}
|
||||
}
|
||||
|
||||
/// 要素を表示
|
||||
pub fn show(&self) {
|
||||
self.set_css("display", "block");
|
||||
}
|
||||
|
||||
/// 要素を非表示
|
||||
pub fn hide(&self) {
|
||||
self.set_css("display", "none");
|
||||
}
|
||||
|
||||
/// スクロールを最下部に移動
|
||||
pub fn scroll_to_bottom(&self) {
|
||||
if let Some(element) = self.get_target_element() {
|
||||
if let Some(html_element) = element.dyn_ref::<HtmlElement>() {
|
||||
html_element.set_scroll_top(html_element.scroll_height());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
impl NyashBox for WebDisplayBox {
|
||||
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
|
||||
fn to_string_box(&self) -> StringBox {
|
||||
StringBox::new(format!("WebDisplayBox({})", self.target_element_id))
|
||||
}
|
||||
|
||||
fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
fn type_name(&self) -> &'static str {
|
||||
"WebDisplayBox"
|
||||
}
|
||||
|
||||
fn box_id(&self) -> u64 {
|
||||
self.id
|
||||
}
|
||||
|
||||
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||
if let Some(other_display) = other.as_any().downcast_ref::<WebDisplayBox>() {
|
||||
BoolBox::new(self.id == other_display.id)
|
||||
} else {
|
||||
BoolBox::new(false)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user