Skip to content

Commit

Permalink
refactor: separate platform-specific interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ksqsf committed Jul 8, 2024
1 parent ceeb8d9 commit a42f132
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
24 changes: 13 additions & 11 deletions include/webview_candidate_window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,7 @@ class WebviewCandidateWindow : public CandidateWindow {
void copy_html();

private:
void *create_window();
void set_transparent_background();
void resize(double dx, double dy, double shadow_top, double shadow_right,
double shadow_bottom, double shadow_left, double width,
double height, double enlarged_width, double enlarged_height,
bool dragging);
void write_clipboard(const std::string &html);
std::shared_ptr<webview::webview> w_;
#ifdef __APPLE__
void *create_listener();
void *listener_;
#endif
double cursor_x_ = 0;
double cursor_y_ = 0;
double x_ = 0;
Expand All @@ -57,6 +46,19 @@ class WebviewCandidateWindow : public CandidateWindow {
layout_t layout_ = layout_t::horizontal;
writing_mode_t writing_mode_ = writing_mode_t::horizontal_tb;

private:
/* Platform-specific interfaces (implemented in 'platform') */
void *create_window();
void set_transparent_background();
void resize(double dx, double dy, double shadow_top, double shadow_right,
double shadow_bottom, double shadow_left, double width,
double height, double enlarged_width, double enlarged_height,
bool dragging);
void write_clipboard(const std::string &html);

void *platform_data = nullptr;
void platform_init();

private:
/* Invoke a JavaScript function. */
template <typename Ret = void, bool debug = false, typename... Args>
Expand Down
2 changes: 2 additions & 0 deletions src/platform/linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace candidate_window {

void WebviewCandidateWindow::platform_init() {}

void *WebviewCandidateWindow::create_window() {
gtk_init(nullptr, nullptr);
auto window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
Expand Down
26 changes: 14 additions & 12 deletions src/platform/macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,32 @@ NSRect getNearestScreenFrame(double x, double y) {
}

namespace candidate_window {
void *WebviewCandidateWindow::create_window() {
auto window =
[[HoverableWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 300)
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:NO];
[window setLevel:NSPopUpMenuWindowLevel];
return window;
}

void *WebviewCandidateWindow::create_listener() {
void WebviewCandidateWindow::platform_init() {
auto listener = [[NotificationListener alloc] init];
[listener setCandidateWindow:this];
[[NSDistributedNotificationCenter defaultCenter]
addObserver:listener
selector:@selector(accentColorChanged:)
name:@"AppleColorPreferencesChangedNotification"
object:nil];
return listener;
platform_data = listener;
}

void *WebviewCandidateWindow::create_window() {
auto window =
[[HoverableWindow alloc] initWithContentRect:NSMakeRect(0, 0, 400, 300)
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:NO];
[window setLevel:NSPopUpMenuWindowLevel];
return window;
}

WebviewCandidateWindow::~WebviewCandidateWindow() {
[(id)w_->window() close]; // By default NSWindow is released on close.
[static_cast<NotificationListener *>(listener_) release];
auto listener = static_cast<NotificationListener *>(this->platform_data);
[listener release];
}

void WebviewCandidateWindow::set_transparent_background() {
Expand Down
8 changes: 2 additions & 6 deletions src/webview_candidate_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ Candidate escape_candidate(const Candidate &c) {
}

WebviewCandidateWindow::WebviewCandidateWindow()
: w_(std::make_shared<webview::webview>(1, create_window()))
#ifdef __APPLE__
,
listener_(create_listener())
#endif
{
: w_(std::make_shared<webview::webview>(1, create_window())) {
platform_init();
set_transparent_background();
update_accent_color();

Expand Down

0 comments on commit a42f132

Please sign in to comment.