Skip to content

Commit

Permalink
refactor: draw message window background using bitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Sep 14, 2024
1 parent 8d4fd13 commit 8100b3e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
6 changes: 3 additions & 3 deletions ChewingTextService/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -817,11 +817,9 @@ void TextService::showCandidates(Ime::EditSession* session) {
if(!candidateWindow_) {
std::wstring bitmap_path = static_cast<ImeModule*>(imeModule())->programDir();
bitmap_path += L"\\Assets\\bubble.9.png";
// candidateWindow_.attach(new Ime::CandidateWindow(this, session, bitmap_path));
HWND parent = this->compositionWindow(session);
candidateWindow_ = nullptr;
CreateCandidateWindow(parent, bitmap_path.c_str(), candidateWindow_.put_void());
// candidateWindow_->setFont(font_);
candidateWindow_->setFontSize(config().fontSize);
}
updateCandidates(session);
Expand All @@ -845,7 +843,9 @@ void TextService::showMessage(Ime::EditSession* session, std::wstring message, i
// FIXME: reuse the window whenever possible
HWND parent = this->compositionWindow(session);
messageWindow_ = nullptr;
CreateMessageWindow(parent, messageWindow_.put_void());
std::wstring bitmap_path = static_cast<ImeModule*>(imeModule())->programDir();
bitmap_path += L"\\Assets\\msg.9.png";
CreateMessageWindow(parent, bitmap_path.c_str(), messageWindow_.put_void());
messageWindow_->setFontSize(config().fontSize);
messageWindow_->setText(message.c_str());

Expand Down
1 change: 1 addition & 0 deletions installer/windows-chewing-tsf.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
</Directory>
<Directory Id="Assets" Name="Assets">
<File Source="assets\bubble.9.png" Bitness="always32" />
<File Source="assets\msg.9.png" Bitness="always32" />
</Directory>
<File Source="x86\chewing-msvc.dll" Bitness="always32" />
<Component Bitness="always32">
Expand Down
2 changes: 1 addition & 1 deletion libIME/idl/libime2.idl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ interface IMessageWindow : IWindow

[local] void LibIME2Init();
[local] void CreateImeWindow([out] void **window);
[local] void CreateMessageWindow(HWND parent, [out] void **messagewindow);
[local] void CreateMessageWindow(HWND parent, [in] LPCWSTR image_path, [out] void **messagewindow);
[local] void CreateCandidateWindow(HWND parent, [in] LPCWSTR image_path, [out] void **candidatewindow);
[local] IWindow *ImeWindowFromHwnd(HWND hwnd);
[local] boolean ImeWindowRegisterClass(HINSTANCE hinstance);
3 changes: 0 additions & 3 deletions libIME/src/window/candidate_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::{
ops::Deref,
};

use log::debug;
use windows::core::*;
use windows::Win32::Foundation::*;
use windows::Win32::Graphics::Direct2D::Common::*;
Expand Down Expand Up @@ -132,7 +131,6 @@ impl CandidateWindow {

unsafe { self.window.resize(width as i32, height as i32) };
self.resize_swap_chain(width, height)?;
debug!("resize_swap_chain {} {}", width, height);

Ok(())
}
Expand Down Expand Up @@ -214,7 +212,6 @@ impl CandidateWindow {
right: rc.right as f32,
bottom: rc.bottom as f32,
};
debug!("rect {:?}", rect);
self.nine_patch_bitmap.draw_bitmap(target, rect)?;

let mut col = 0;
Expand Down
22 changes: 16 additions & 6 deletions libIME/src/window/message_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct MessageWindow {
swapchain: RefCell<Option<IDXGISwapChain1>>,
dcomptarget: RefCell<Option<IDCompositionTarget>>,
brush: RefCell<Option<ID2D1SolidColorBrush>>,
nine_patch_bitmap: NinePatchBitmap,
dpi: f32,
window: ComObject<Window>,
}
Expand All @@ -54,8 +55,7 @@ impl MessageWindow {
let mut metrics = DWRITE_TEXT_METRICS::default();
unsafe { text_layout.GetMetrics(&mut metrics).unwrap() };

// FIXME
let margin = 5.0;
let margin = self.nine_patch_bitmap.margin();
let width = metrics.width + margin * 2.0;
let height = metrics.height + margin * 2.0;
unsafe {
Expand Down Expand Up @@ -147,14 +147,21 @@ impl MessageWindow {
GetClientRect(self.window.hwnd.get(), &mut rc)?;

target.BeginDraw();
target.Clear(Some(&create_color(COLOR_INFOBK)));
let rect = D2D_RECT_F {
top: rc.top as f32,
left: rc.left as f32,
right: rc.right as f32,
bottom: rc.bottom as f32,
};
self.nine_patch_bitmap.draw_bitmap(target, rect)?;
let margin = self.nine_patch_bitmap.margin();

target.DrawText(
self.text.borrow().as_wide(),
self.text_format.borrow().deref(),
&D2D_RECT_F {
left: 5.0,
top: 5.0,
left: margin,
top: margin,
right: f32::MAX,
bottom: f32::MAX,
},
Expand Down Expand Up @@ -183,7 +190,7 @@ impl MessageWindow {
}

#[no_mangle]
unsafe extern "C" fn CreateMessageWindow(parent: HWND, ret: *mut *mut c_void) {
unsafe extern "C" fn CreateMessageWindow(parent: HWND, image_path: PCWSTR, ret: *mut *mut c_void) {
let window = Window::new().into_object();
window.create(
parent,
Expand Down Expand Up @@ -214,6 +221,8 @@ unsafe extern "C" fn CreateMessageWindow(parent: HWND, ret: *mut *mut c_void) {
)
.unwrap();

let nine_patch_bitmap = NinePatchBitmap::new(image_path).unwrap();

let message_window = MessageWindow {
text: RefCell::new(h!("").to_owned()),
factory,
Expand All @@ -223,6 +232,7 @@ unsafe extern "C" fn CreateMessageWindow(parent: HWND, ret: *mut *mut c_void) {
target: None.into(),
swapchain: None.into(),
brush: None.into(),
nine_patch_bitmap,
dpi,
window,
}
Expand Down

0 comments on commit 8100b3e

Please sign in to comment.