Skip to content

Commit

Permalink
refactor: reimplement MessageWindow in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Sep 11, 2024
1 parent 99ceabe commit 9445bad
Show file tree
Hide file tree
Showing 12 changed files with 992 additions and 303 deletions.
40 changes: 14 additions & 26 deletions ChewingTextService/ChewingTextService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
#include <libIME/LangBarButton.h>
#include <libIME/Utils.h>
#include <sys/stat.h>
#include <winrt/base.h>

#include <cstddef>
#include <string>

#include "ChewingImeModule.h"
#include "resource.h"
#include "libime2.h"


using namespace std;
Expand Down Expand Up @@ -70,7 +73,6 @@ TextService::TextService(ImeModule* module):
shapeMode_(-1),
outputSimpChinese_(false),
lastKeyDownCode_(0),
messageWindow_(NULL),
messageTimerId_(0),
candidateWindow_(NULL),
imeModeIcon_(NULL),
Expand Down Expand Up @@ -722,9 +724,10 @@ void TextService::applyConfig() {
::DeleteObject(font_); // delete old font
lf.lfHeight = cfg.fontSize; // apply the new size
font_ = CreateFontIndirect(&lf); // create new font
if(messageWindow_)
messageWindow_->setFont(font_);
// messageWindow_->setFontSize(cfg.fontSize);
if(messageWindow_) {
// messageWindow_->setFont(font_);
messageWindow_->setFontSize(cfg.fontSize);
}
if(candidateWindow_) {
candidateWindow_->setFont(font_);
candidateWindow_->setFontSize(static_cast<float>(cfg.fontSize));
Expand Down Expand Up @@ -833,10 +836,11 @@ void TextService::showMessage(Ime::EditSession* session, std::wstring message, i
// remove previous message if there's any
hideMessage();
// FIXME: reuse the window whenever possible
messageWindow_ = new Ime::MessageWindow(this, session);
messageWindow_->setFont(font_);
HWND parent = this->compositionWindow(session);
messageWindow_ = nullptr;
CreateMessageWindow(parent, messageWindow_.put_void());
messageWindow_->setFontSize(config().fontSize);
messageWindow_->setText(message);
messageWindow_->setText(message.c_str());

int x = 0, y = 0;
if(isComposing()) {
Expand All @@ -849,7 +853,7 @@ void TextService::showMessage(Ime::EditSession* session, std::wstring message, i
messageWindow_->move(x, y);
messageWindow_->show();

messageTimerId_ = ::SetTimer(messageWindow_->hwnd(), 1, duration * 1000, (TIMERPROC)TextService::onMessageTimeout);
messageTimerId_ = ::SetTimer(messageWindow_->hwnd(), 1, duration * 1000, nullptr);
}

void TextService::hideMessage() {
Expand All @@ -858,27 +862,11 @@ void TextService::hideMessage() {
messageTimerId_ = 0;
}
if(messageWindow_) {
delete messageWindow_;
messageWindow_ = NULL;
}
}

// called when the message window timeout
void TextService::onMessageTimeout() {
hideMessage();
}

// static
void CALLBACK TextService::onMessageTimeout(HWND hwnd, UINT msg, UINT_PTR id, DWORD time) {
Ime::MessageWindow* messageWindow = (Ime::MessageWindow*)Ime::Window::fromHwnd(hwnd);
assert(messageWindow);
if(messageWindow) {
TextService* pThis = (Chewing::TextService*)messageWindow->textService();
pThis->onMessageTimeout();
messageWindow_->destroy();
messageWindow_ = nullptr;
}
}


void TextService::updateLangButtons() {
if(!chewingContext_)
return;
Expand Down
10 changes: 6 additions & 4 deletions ChewingTextService/ChewingTextService.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@

#include <LibIME/TextService.h>
#include <LibIME/CandidateWindow.h>
#include <LibIME/MessageWindow.h>
#include <LibIME/EditSession.h>
#include <LibIME/LangBarButton.h>
#include <chewing.h>
#include "ChewingImeModule.h"
#include <sys/types.h>

#include <Unknwn.h>
#include <winrt/base.h>

#include "libime2.h"

namespace Chewing {

class TextService: public Ime::TextService {
Expand Down Expand Up @@ -91,8 +95,6 @@ class TextService: public Ime::TextService {
// message window
void showMessage(Ime::EditSession* session, std::wstring message, int duration = 3);
void hideMessage();
void onMessageTimeout();
static void CALLBACK onMessageTimeout(HWND hwnd, UINT msg, UINT_PTR id, DWORD time);

void updateLangButtons(); // update status of language bar buttons

Expand All @@ -110,7 +112,7 @@ class TextService: public Ime::TextService {
ChewingContext* chewingContext_;
Ime::CandidateWindow* candidateWindow_;
bool showingCandidates_;
Ime::MessageWindow* messageWindow_;
winrt::com_ptr<IMessageWindow> messageWindow_;
UINT messageTimerId_;
HFONT font_;

Expand Down
23 changes: 21 additions & 2 deletions libIME/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_CXX_SIMULATE_ID MATCHES "MSVC
corrosion_set_env_vars(libime2 "CFLAGS=-EHsc" "CXXFLAGS=-EHsc")
endif()

find_program(MIDL midl)
add_custom_command(
OUTPUT
dlldata.c
libime2.h
libime2_i.c
libime2_p.c
COMMAND MIDL ${CMAKE_CURRENT_SOURCE_DIR}/idl/libime2.idl
MAIN_DEPENDENCY idl/libime2.idl
)

add_library(libIME_static STATIC
# Core TSF part
ImeModule.cpp
Expand Down Expand Up @@ -49,19 +60,27 @@ add_library(libIME_static STATIC
Window.h
ImeWindow.cpp
ImeWindow.h
MessageWindow.cpp
MessageWindow.h
CandidateWindow.h
CandidateWindow.cpp
NinePatch.h
NinePatch.cpp

dlldata.c
libime2.h
libime2_i.c
libime2_p.c
)

target_include_directories(libIME_static PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

target_link_libraries(libIME_static
PUBLIC libime2_bridge
PUBLIC shlwapi.lib
PUBLIC d2d1.lib
PUBLIC d3d11.lib
PUBLIC dwrite.lib
PUBLIC dcomp.lib

PUBLIC Propsys.lib
PUBLIC RuntimeObject.lib
)
130 changes: 130 additions & 0 deletions libIME/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions libIME/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,18 @@ cxx = { version = "1.0.128", features = ["c++17"] }
log = "0.4.22"
nine_patch_drawable = "0.1.0"
win_dbg_logger = "0.1.0"
windows-core = "0.58.0"
windows = { version = "0.58.0", features = [
"implement",
"Foundation_Numerics",
"Win32_Graphics_Direct2D",
"Win32_Graphics_Direct2D_Common",
"Win32_Graphics_Direct3D",
"Win32_Graphics_Direct3D11",
"Win32_Graphics_DirectComposition",
"Win32_Graphics_DirectWrite",
"Win32_Graphics_Dxgi",
"Win32_Graphics_Dxgi_Common",
"Win32_Graphics_Gdi",
"Win32_UI_WindowsAndMessaging",
] }
4 changes: 4 additions & 0 deletions libIME/ImeModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "TextService.h"
#include "DisplayAttributeProvider.h"

#include "libime2.h"

using namespace std;

namespace Ime {
Expand Down Expand Up @@ -64,7 +66,9 @@ ImeModule::ImeModule(HMODULE module, const CLSID& textServiceClsid):
textServiceClsid_(textServiceClsid),
refCount_(1) {

LibIME2Init();
Window::registerClass(hInstance_);
ImeWindowRegisterClass(hInstance_);

// regiser default display attributes
inputAttrib_ = new DisplayAttributeInfo(g_inputDisplayAttributeGuid);
Expand Down
Loading

0 comments on commit 9445bad

Please sign in to comment.