Skip to content

armgit / WacOS

Public Imported from GitHub
0

A new style desktop shell for Windows 11: menu bar, Dock, Finder, Spotlight, Control Center and Liquid Glass, in native C++.

Rust
2.8 KB
Raw Blame
//! comum\composicao.h: teclados com composição (coreano, japonês, chinês) nos campos desenhados pelo Shell. O texto em
//! composição entra direto no fim do campo (e já é buscado) e é trocado pelo resultado quando o teclado confirma.
#![allow(dead_code)]

use comum::windows::Win32::Foundation::*;
use comum::windows::Win32::UI::Input::Ime::*;
use comum::windows::Win32::UI::WindowsAndMessaging::*;

#[derive(Default)]
pub struct Composicao {
    /// o que está no fim do texto agora, ainda em composição
    atual: String,
}
impl Composicao {
    fn tira(&mut self, texto: &mut String) {
        if !self.atual.is_empty() && texto.ends_with(&self.atual) {
            let n = texto.len() - self.atual.len();
            texto.truncate(n);
        }
        self.atual.clear();
    }
    /// true = a mensagem era da composição (quem chamou devolve 0); `mudou` = o texto mudou
    pub fn trata(&mut self, h: HWND, m: u32, l: LPARAM, texto: &mut String, mudou: &mut bool) -> bool {
        if m == WM_IME_STARTCOMPOSITION {
            self.atual.clear();
            return true;
        }
        if m == WM_IME_ENDCOMPOSITION {
            if !self.atual.is_empty() {
                self.tira(texto);
                *mudou = true;
            }
            return true;
        }
        if m != WM_IME_COMPOSITION {
            return false;
        }
        unsafe {
            let c = ImmGetContext(h);
            if c.is_invalid() {
                return false;
            }
            let ler = |k: IME_COMPOSITION_STRING| {
                let n = ImmGetCompositionStringW(c, k, None, 0);
                if n <= 0 {
                    return String::new();
                }
                let mut b = vec![0u16; n as usize / 2];
                ImmGetCompositionStringW(c, k, Some(b.as_mut_ptr() as *mut _), n as u32);
                String::from_utf16_lossy(&b)
            };
            self.tira(texto);
            if l.0 as u32 & GCS_RESULTSTR.0 != 0 {
                *texto += &ler(GCS_RESULTSTR);
            }
            if l.0 as u32 & GCS_COMPSTR.0 != 0 {
                self.atual = ler(GCS_COMPSTR);
                *texto += &self.atual;
            }
            let _ = ImmReleaseContext(h, c);
        }
        *mudou = true;
        true
    }
    /// fecha o painel ou limpa o campo: a composição pela metade é descartada (no teclado também)
    pub fn cancela(&mut self, h: HWND, texto: &mut String) {
        if self.atual.is_empty() {
            return;
        }
        self.tira(texto);
        unsafe {
            let c = ImmGetContext(h);
            if !c.is_invalid() {
                let _ = ImmNotifyIME(c, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
                let _ = ImmReleaseContext(h, c);
            }
        }
    }
}