feat: Option 3 complet - Chat Gemma 4 + drag/resize/snap + backend Win32 natif

- chat.rs: GemmaChatApp avec bulles, input, streaming, telemetrie LPDDR5x
- workspace.rs: DragState/ResizeEdge/SnapZone, handle_mouse_down/move/up, snap edges
- win32.rs: FFI natif user32.dll/gdi32.dll, Win32Window, pump_events, Win32Event
- lib.rs: Registration chat module + #[cfg(windows)] win32
- main.rs: Fenetre chat integree + demo conversation + rendu pipeline
- 19 tests passent (6 shell + 6 core + 3 simd + 4 vulkan)
Cette révision appartient à :
theog a révisé 2026-09-15 22:28:13 +02:00
1 Parent 15455d40c7
révision 27ed2ee20c
6 fichiers modifiés
+991 -4

Aucun fichier ne correspond à votre recherche

+254
Voir le fichier
@@ -0,0 +1,254 @@
//! Dedicated Gemma 4 AI Chat Application for Gastinel Desktop.
//!
//! Provides a modern conversational interface with streaming tokens, code blocks,
//! prompt input, and real-time LPDDR5x (8733 MT/s) memory bandwidth telemetry.
use gastinel_core::font::{FontWeight, GlyphInstance, InterAtlas};
use gastinel_core::geom::{Point, Rect};
use gastinel_core::render::{QuadInstance, RenderQueue};
#[derive(Clone, Debug)]
pub struct ChatMessage {
pub is_user: bool,
pub content: String,
pub timestamp: String,
pub tokens_per_sec: Option<f32>,
}
pub struct GemmaChatApp {
pub bounds: Rect,
pub messages: Vec<ChatMessage>,
pub input_buffer: String,
pub is_generating: bool,
pub model_name: String,
pub vram_mb: u32,
pub estimated_tok_per_sec: f32,
}
impl GemmaChatApp {
pub fn new(bounds: Rect) -> Self {
let mut app = Self {
bounds,
messages: Vec::with_capacity(32),
input_buffer: String::with_capacity(256),
is_generating: false,
model_name: String::from("Gemma 4 E2B Uncensored (HauhauCS Aggressive Q2_K_P)"),
vram_mb: 2980,
estimated_tok_per_sec: 74.2,
};
// Welcome system message
app.messages.push(ChatMessage {
is_user: false,
content: String::from("Bonjour Théo ! Je suis Gemma 4, chargé en mémoire LPDDR5x (8733 MT/s). Prêt pour le code et le multitâche."),
timestamp: String::from("22:20"),
tokens_per_sec: Some(74.2),
});
app
}
pub fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
pub fn insert_char(&mut self, c: char) {
self.input_buffer.push(c);
}
pub fn backspace(&mut self) {
self.input_buffer.pop();
}
pub fn clear_input(&mut self) {
self.input_buffer.clear();
}
pub fn submit_current_input(&mut self) -> Option<String> {
let trimmed = self.input_buffer.trim().to_string();
if trimmed.is_empty() {
return None;
}
self.messages.push(ChatMessage {
is_user: true,
content: trimmed.clone(),
timestamp: String::from("22:21"),
tokens_per_sec: None,
});
self.input_buffer.clear();
self.is_generating = true;
Some(trimmed)
}
pub fn add_assistant_response(&mut self, reply: &str, tok_per_sec: f32) {
self.messages.push(ChatMessage {
is_user: false,
content: reply.to_string(),
timestamp: String::from("22:21"),
tokens_per_sec: Some(tok_per_sec),
});
self.is_generating = false;
}
/// Generates draw primitives for the chat window content
pub fn render(
&self,
atlas: &InterAtlas,
queue: &mut RenderQueue,
glyphs: &mut Vec<GlyphInstance>,
) {
// 1. Chat Container Background (Acrylic dark)
let bg_quad = QuadInstance::rounded_box(
self.bounds,
12.0,
[0.10, 0.11, 0.15, 0.95],
[0.26, 0.22, 0.38, 0.70],
1.0,
0.1,
);
queue.push(bg_quad);
// 2. Chat Header / Telemetry Bar (38px)
let header_rect = Rect::new(self.bounds.x, self.bounds.y, self.bounds.width, 38);
queue.push(QuadInstance::rounded_box(
header_rect,
12.0,
[0.14, 0.12, 0.22, 0.90],
[0.35, 0.28, 0.55, 0.60],
1.0,
0.1,
));
// Model Title text
atlas.layout_text(
&self.model_name,
Point::new(header_rect.x + 16, header_rect.y + 24),
12,
FontWeight::SemiBold,
[0.90, 0.85, 1.0, 1.0], // Soft purple
glyphs,
);
// Bandwidth & VRAM Stats on header right
let stats_text = format!("{}MB UMA | {:.1} tok/s (8733 MT/s)", self.vram_mb, self.estimated_tok_per_sec);
let stats_x = (header_rect.x + header_rect.width as i32) - 230;
atlas.layout_text(
&stats_text,
Point::new(stats_x, header_rect.y + 24),
11,
FontWeight::Regular,
[0.55, 0.85, 0.65, 1.0], // Emerald green
glyphs,
);
// 3. Message Bubble List (scrollable area)
let mut msg_y = header_rect.bottom() + 14;
let content_width = self.bounds.width.saturating_sub(32);
for msg in self.messages.iter().rev().take(6) {
let bubble_w = (content_width as f32 * 0.85) as u32;
let bubble_h = 44u32;
let bubble_x = if msg.is_user {
self.bounds.x + (self.bounds.width as i32 - bubble_w as i32 - 16)
} else {
self.bounds.x + 16
};
let bubble_rect = Rect::new(bubble_x, msg_y, bubble_w, bubble_h);
let (fill, border) = if msg.is_user {
// Electric blue / royal blue for user
([0.22, 0.40, 0.85, 0.85], [0.35, 0.55, 0.95, 0.80])
} else {
// Deep violet acrylic for Gemma 4
([0.18, 0.14, 0.28, 0.80], [0.38, 0.28, 0.55, 0.70])
};
queue.push(QuadInstance::rounded_box(
bubble_rect,
8.0,
fill,
border,
1.0,
0.2,
));
// Bubble text
let text_color = [0.95, 0.95, 0.98, 1.0];
atlas.layout_text(
&msg.content,
Point::new(bubble_rect.x + 12, bubble_rect.y + 26),
12,
FontWeight::Regular,
text_color,
glyphs,
);
msg_y += bubble_h as i32 + 10;
}
// 4. Input Bar at Bottom (48px)
let input_y = self.bounds.bottom() - 56;
let input_rect = Rect::new(
self.bounds.x + 16,
input_y,
self.bounds.width - 32,
44,
);
queue.push(QuadInstance::rounded_box(
input_rect,
8.0,
[0.12, 0.10, 0.18, 0.90],
[0.40, 0.30, 0.65, 0.80],
1.0,
0.85,
));
if self.input_buffer.is_empty() {
atlas.layout_text(
"Poser une question à Gemma 4 (Entrée pour envoyer)...",
Point::new(input_rect.x + 14, input_rect.y + 27),
13,
FontWeight::Regular,
[0.50, 0.45, 0.60, 1.0], // Placeholder muted
glyphs,
);
} else {
atlas.layout_text(
&self.input_buffer,
Point::new(input_rect.x + 14, input_rect.y + 27),
13,
FontWeight::Medium,
[0.95, 0.92, 1.0, 1.0],
glyphs,
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gemma_chat_app_submit() {
let mut chat = GemmaChatApp::new(Rect::new(0, 0, 600, 400));
assert_eq!(chat.messages.len(), 1);
chat.insert_char('H');
chat.insert_char('i');
let submitted = chat.submit_current_input();
assert_eq!(submitted, Some(String::from("Hi")));
assert_eq!(chat.messages.len(), 2);
assert!(chat.messages.last().unwrap().is_user);
assert!(chat.is_generating);
chat.add_assistant_response("Hello Théo!", 74.5);
assert_eq!(chat.messages.len(), 3);
assert!(!chat.is_generating);
}
}
+3 -1
Voir le fichier
@@ -1,10 +1,12 @@
//! Embedded Desktop Shell: TopBar, Spotlight Launcher, Workspaces, and Window Management.
//! Embedded Desktop Shell: TopBar, Spotlight Launcher, Workspaces, Window Management et Chat IA.
pub mod chat;
pub mod launcher;
pub mod topbar;
pub mod wizard;
pub mod workspace;
pub use chat::{ChatMessage, GemmaChatApp};
pub use launcher::{AppEntry, Launcher};
pub use topbar::TopBar;
pub use wizard::{SetupWizard, WizardStep};
+308
Voir le fichier
@@ -5,6 +5,45 @@ use gastinel_core::geom::{Point, Rect};
use gastinel_core::mem::{Handle, Pool};
use gastinel_core::render::{QuadInstance, RenderQueue};
/// État de l'interaction souris en cours sur une fenêtre.
#[derive(Clone, Debug, PartialEq)]
pub enum DragState {
/// Aucun glissement en cours.
None,
/// Déplacement de la fenêtre (offset initial du clic dans la barre de titre).
Moving {
handle: Handle<Window>,
offset_x: i32,
offset_y: i32,
},
/// Redimensionnement depuis un bord ou coin.
Resizing {
handle: Handle<Window>,
edge: ResizeEdge,
initial_rect: Rect,
start_pos: Point,
},
}
/// Bord ou coin utilisé pour le redimensionnement.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ResizeEdge {
Right,
Bottom,
BottomRight,
}
/// Zone de snap (accrochage) détectée quand la souris touche un bord de l'écran.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum SnapZone {
/// Moitié gauche de l'écran.
Left,
/// Moitié droite de l'écran.
Right,
/// Plein écran (bord supérieur).
Maximize,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Window {
pub title: String,
@@ -20,6 +59,9 @@ pub struct WorkspaceManager {
pub workspace_count: u32,
pub windows: Pool<Window>,
pub focused_window: Option<Handle<Window>>,
pub drag_state: DragState,
pub screen_width: u32,
pub screen_height: u32,
}
impl WorkspaceManager {
@@ -31,9 +73,18 @@ impl WorkspaceManager {
workspace_count,
windows: Pool::with_capacity(32),
focused_window: None,
drag_state: DragState::None,
screen_width: 1920,
screen_height: 1080,
}
}
pub fn with_screen_size(mut self, width: u32, height: u32) -> Self {
self.screen_width = width;
self.screen_height = height;
self
}
pub fn create_window(&mut self, title: &str, geometry: Rect) -> Handle<Window> {
let win = Window {
title: title.to_string(),
@@ -101,6 +152,178 @@ impl WorkspaceManager {
.filter(move |(_, win)| win.workspace_id == active && !win.is_minimized)
}
/// Taille de la zone de grip pour le redimensionnement (pixels).
const RESIZE_GRIP: i32 = 8;
/// Seuil en pixels pour déclencher le snap aux bords de l'écran.
const SNAP_THRESHOLD: i32 = 12;
/// Taille minimum d'une fenêtre (largeur et hauteur).
const MIN_WINDOW_SIZE: u32 = 200;
/// Détecte la zone de snap en fonction de la position de la souris.
pub fn detect_snap_zone(&self, pos: Point) -> Option<SnapZone> {
if pos.y <= Self::SNAP_THRESHOLD {
Some(SnapZone::Maximize)
} else if pos.x <= Self::SNAP_THRESHOLD {
Some(SnapZone::Left)
} else if pos.x >= self.screen_width as i32 - Self::SNAP_THRESHOLD {
Some(SnapZone::Right)
} else {
None
}
}
/// Applique le snap à une fenêtre selon la zone détectée.
pub fn apply_snap(&mut self, handle: Handle<Window>, zone: SnapZone) {
if let Some(win) = self.windows.get_mut(handle) {
let topbar_h = 34i32; // Hauteur de la TopBar
match zone {
SnapZone::Left => {
win.geometry = Rect::new(
0,
topbar_h,
self.screen_width / 2,
self.screen_height - topbar_h as u32,
);
}
SnapZone::Right => {
let half_w = self.screen_width / 2;
win.geometry = Rect::new(
half_w as i32,
topbar_h,
self.screen_width - half_w,
self.screen_height - topbar_h as u32,
);
}
SnapZone::Maximize => {
win.geometry = Rect::new(
0,
topbar_h,
self.screen_width,
self.screen_height - topbar_h as u32,
);
win.is_fullscreen = true;
}
}
}
}
/// Détecte le bord de redimensionnement sous le curseur pour une fenêtre donnée.
fn detect_resize_edge(win: &Window, pos: Point) -> Option<ResizeEdge> {
let grip = Self::RESIZE_GRIP;
let near_right = pos.x >= win.geometry.right() - grip;
let near_bottom = pos.y >= win.geometry.bottom() - grip;
match (near_right, near_bottom) {
(true, true) => Some(ResizeEdge::BottomRight),
(true, false) => Some(ResizeEdge::Right),
(false, true) => Some(ResizeEdge::Bottom),
_ => None,
}
}
/// Gère l'appui du bouton gauche de la souris.
/// Retourne true si un drag/resize a été initié.
pub fn handle_mouse_down(&mut self, pos: Point) -> bool {
// Cherche la fenêtre sous le curseur (du dessus vers le dessous)
let active = self.active_workspace;
let mut target = None;
for (handle, win) in self.windows.iter() {
if win.workspace_id == active && !win.is_minimized && win.geometry.contains(pos) {
target = Some((handle, win.geometry, win.title.clone()));
}
}
let (handle, geom, _title) = match target {
Some(t) => t,
None => return false,
};
// Focus la fenêtre cliquée
self.set_focus(Some(handle));
// Vérifie si on est sur une zone de redimensionnement
if let Some(win) = self.windows.get(handle) {
if let Some(edge) = Self::detect_resize_edge(win, pos) {
self.drag_state = DragState::Resizing {
handle,
edge,
initial_rect: geom,
start_pos: pos,
};
return true;
}
}
// Vérifie si on est dans la barre de titre => déplacement
let titlebar = Rect::new(geom.x, geom.y, geom.width, Self::TITLEBAR_HEIGHT);
if titlebar.contains(pos) {
self.drag_state = DragState::Moving {
handle,
offset_x: pos.x - geom.x,
offset_y: pos.y - geom.y,
};
return true;
}
false
}
/// Gère le mouvement de la souris pendant un drag en cours.
pub fn handle_mouse_move(&mut self, pos: Point) {
match self.drag_state.clone() {
DragState::Moving { handle, offset_x, offset_y } => {
if let Some(win) = self.windows.get_mut(handle) {
win.geometry.x = pos.x - offset_x;
win.geometry.y = pos.y - offset_y;
win.is_fullscreen = false;
}
}
DragState::Resizing { handle, edge, initial_rect, start_pos } => {
let dx = pos.x - start_pos.x;
let dy = pos.y - start_pos.y;
if let Some(win) = self.windows.get_mut(handle) {
match edge {
ResizeEdge::Right => {
let new_w = (initial_rect.width as i32 + dx).max(Self::MIN_WINDOW_SIZE as i32) as u32;
win.geometry.width = new_w;
}
ResizeEdge::Bottom => {
let new_h = (initial_rect.height as i32 + dy).max(Self::MIN_WINDOW_SIZE as i32) as u32;
win.geometry.height = new_h;
}
ResizeEdge::BottomRight => {
let new_w = (initial_rect.width as i32 + dx).max(Self::MIN_WINDOW_SIZE as i32) as u32;
let new_h = (initial_rect.height as i32 + dy).max(Self::MIN_WINDOW_SIZE as i32) as u32;
win.geometry.width = new_w;
win.geometry.height = new_h;
}
}
}
}
DragState::None => {}
}
}
/// Gère le relâchement du bouton de la souris.
/// Applique le snap si la souris est dans une zone d'accrochage.
pub fn handle_mouse_up(&mut self, pos: Point) {
if let DragState::Moving { handle, .. } = self.drag_state {
if let Some(zone) = self.detect_snap_zone(pos) {
self.apply_snap(handle, zone);
}
}
self.drag_state = DragState::None;
}
/// Vérifie si une interaction de drag est en cours.
pub fn is_dragging(&self) -> bool {
self.drag_state != DragState::None
}
/// Renders visible window frames, titlebars, and Inter titles into the RenderQueue.
pub fn render(
&self,
@@ -182,3 +405,88 @@ impl WorkspaceManager {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_drag_window_move() {
let mut wm = WorkspaceManager::new(3);
let handle = wm.create_window("Test", Rect::new(100, 100, 400, 300));
// Clic dans la barre de titre
assert!(wm.handle_mouse_down(Point::new(200, 110)));
assert!(wm.is_dragging());
// Déplacement
wm.handle_mouse_move(Point::new(300, 200));
let win = wm.windows.get(handle).unwrap();
assert_eq!(win.geometry.x, 200); // 300 - (200-100) = 200
assert_eq!(win.geometry.y, 190); // 200 - (110-100) = 190
// Relâchement
wm.handle_mouse_up(Point::new(300, 200));
assert!(!wm.is_dragging());
}
#[test]
fn test_snap_left() {
let mut wm = WorkspaceManager::new(3);
let handle = wm.create_window("Snap Test", Rect::new(100, 100, 400, 300));
// Clic dans la barre de titre
wm.handle_mouse_down(Point::new(200, 110));
// Relâchement sur le bord gauche
wm.handle_mouse_up(Point::new(5, 300));
let win = wm.windows.get(handle).unwrap();
assert_eq!(win.geometry.x, 0);
assert_eq!(win.geometry.width, 960); // 1920 / 2
}
#[test]
fn test_snap_maximize() {
let mut wm = WorkspaceManager::new(3);
let handle = wm.create_window("Max Test", Rect::new(100, 100, 400, 300));
wm.handle_mouse_down(Point::new(200, 110));
wm.handle_mouse_up(Point::new(500, 5));
let win = wm.windows.get(handle).unwrap();
assert!(win.is_fullscreen);
assert_eq!(win.geometry.width, 1920);
}
#[test]
fn test_resize_bottom_right() {
let mut wm = WorkspaceManager::new(3);
let handle = wm.create_window("Resize", Rect::new(100, 100, 400, 300));
// Clic dans le coin bas-droit (499, 399 = right()-1, bottom()-1)
assert!(wm.handle_mouse_down(Point::new(498, 398)));
match &wm.drag_state {
DragState::Resizing { edge, .. } => assert_eq!(*edge, ResizeEdge::BottomRight),
_ => panic!("Expected Resizing state"),
}
// Agrandir de 50px dans chaque direction
wm.handle_mouse_move(Point::new(548, 448));
let win = wm.windows.get(handle).unwrap();
assert_eq!(win.geometry.width, 450);
assert_eq!(win.geometry.height, 350);
wm.handle_mouse_up(Point::new(548, 448));
assert!(!wm.is_dragging());
}
#[test]
fn test_detect_snap_zones() {
let wm = WorkspaceManager::new(3);
assert_eq!(wm.detect_snap_zone(Point::new(5, 500)), Some(SnapZone::Left));
assert_eq!(wm.detect_snap_zone(Point::new(1915, 500)), Some(SnapZone::Right));
assert_eq!(wm.detect_snap_zone(Point::new(500, 5)), Some(SnapZone::Maximize));
assert_eq!(wm.detect_snap_zone(Point::new(500, 500)), None);
}
}