Skip to content

Commit

Permalink
feat(tsf): enable language bar button (#170)
Browse files Browse the repository at this point in the history
* feat(tsf): enable language bar icon

* feat(server): show deploy notification even if tray icon is disabled

* feat(tsf): update language bar button on initializing

* refactor(resource.h): unify resource.h

* feat(tsf): switch ascii mode on language bar icon click

* chore(tsf): change menu description
  • Loading branch information
nameoverflow authored and lotem committed Apr 7, 2018
1 parent 22795b5 commit 2b66039
Show file tree
Hide file tree
Showing 22 changed files with 105 additions and 68 deletions.
10 changes: 9 additions & 1 deletion RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ UINT RimeWithWeaselHandler::FindSession(UINT session_id)
return found ? session_id : 0;
}

UINT RimeWithWeaselHandler::AddSession(LPWSTR buffer)
UINT RimeWithWeaselHandler::AddSession(LPWSTR buffer, EatLine eat)
{
if (m_disabled)
{
Expand All @@ -104,6 +104,9 @@ UINT RimeWithWeaselHandler::AddSession(LPWSTR buffer)
DLOG(INFO) << "Add session: created session_id = " << session_id;
_ReadClientInfo(session_id, buffer);
// show session's welcome message :-) if any
if (eat) {
_Respond(session_id, eat);
}
_UpdateUI(session_id);
m_active_session = session_id;
return session_id;
Expand Down Expand Up @@ -297,6 +300,11 @@ void RimeWithWeaselHandler::EndMaintenance()
}
}

void RimeWithWeaselHandler::SetOption(UINT session_id, const std::string & opt, bool val)
{
RimeSetOption(session_id, opt.c_str(), val);
}

void RimeWithWeaselHandler::OnUpdateUI(std::function<void()> const &cb)
{
_UpdateUICallback = cb;
Expand Down
10 changes: 10 additions & 0 deletions WeaselIPC/WeaselClientImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ void ClientImpl::FocusOut()
_SendMessage(WEASEL_IPC_FOCUS_OUT, 0, session_id);
}

void ClientImpl::TrayCommand(UINT menuId)
{
_SendMessage(WEASEL_IPC_TRAY_COMMAND, menuId, session_id);
}

void ClientImpl::StartSession()
{
if (_Active() && Echo())
Expand Down Expand Up @@ -274,6 +279,11 @@ void Client::EndMaintenance()
m_pImpl->EndMaintenance();
}

void Client::TrayCommand(UINT menuId)
{
m_pImpl->TrayCommand(menuId);
}

bool Client::Echo()
{
return m_pImpl->Echo();
Expand Down
1 change: 1 addition & 0 deletions WeaselIPC/WeaselClientImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace weasel
void UpdateInputPosition(RECT const& rc);
void FocusIn();
void FocusOut();
void TrayCommand(UINT menuId);
bool GetResponseData(ResponseHandler const& handler);

protected:
Expand Down
28 changes: 26 additions & 2 deletions WeaselIPCServer/WeaselServerImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "stdafx.h"
#include "WeaselServerImpl.h"
#include <Windows.h>
#include <boost/thread.hpp>
#include <VersionHelpers.hpp>
#include <resource.h>

namespace weasel {
class PipeServer : public PipeChannel<DWORD, PipeMessage>
Expand Down Expand Up @@ -89,6 +89,16 @@ LRESULT ServerImpl::OnEndSystemSession(UINT uMsg, WPARAM wParam, LPARAM lParam,
LRESULT ServerImpl::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
UINT uID = LOWORD(wParam);
switch (uID) {
case ID_WEASELTRAY_ENABLE_ASCII:
m_pRequestHandler->SetOption(lParam, "ascii_mode", true);
return 0;
case ID_WEASELTRAY_DISABLE_ASCII:
m_pRequestHandler->SetOption(lParam, "ascii_mode", false);
return 0;
default:;
}

std::map<UINT, CommandHandler>::iterator it = m_MenuHandlers.find(uID);
if (it == m_MenuHandlers.end())
{
Expand All @@ -99,6 +109,13 @@ LRESULT ServerImpl::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHa
return 0;
}

DWORD ServerImpl::OnCommand(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
{
BOOL handled = TRUE;
OnCommand(uMsg, wParam, lParam, handled);
return handled;
}

int ServerImpl::Start()
{
// assure single instance
Expand Down Expand Up @@ -159,7 +176,13 @@ DWORD ServerImpl::OnStartSession(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lP
{
if (!m_pRequestHandler)
return 0;
return m_pRequestHandler->AddSession(reinterpret_cast<LPWSTR>(channel->ReceiveBuffer()));
return m_pRequestHandler->AddSession(
reinterpret_cast<LPWSTR>(channel->ReceiveBuffer()),
[this](std::wstring &msg) -> bool {
*channel << msg;
return true;
}
);
}

DWORD ServerImpl::OnEndSession(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam)
Expand Down Expand Up @@ -302,6 +325,7 @@ void ServerImpl::HandlePipeMessage(PipeMessage pipe_msg, _Resp resp)
PIPE_MSG_HANDLE(WEASEL_IPC_END_MAINTENANCE, OnEndMaintenance)
PIPE_MSG_HANDLE(WEASEL_IPC_COMMIT_COMPOSITION, OnCommitComposition)
PIPE_MSG_HANDLE(WEASEL_IPC_CLEAR_COMPOSITION, OnClearComposition);
PIPE_MSG_HANDLE(WEASEL_IPC_TRAY_COMMAND, OnCommand);
END_MAP_PIPE_MSG_HANDLE(result);

resp(result);
Expand Down
1 change: 1 addition & 0 deletions WeaselIPCServer/WeaselServerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ namespace weasel
LRESULT OnQueryEndSystemSession(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnEndSystemSession(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
LRESULT OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
DWORD OnCommand(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnEcho(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnStartSession(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
DWORD OnEndSession(WEASEL_IPC_COMMAND uMsg, DWORD wParam, DWORD lParam);
Expand Down
2 changes: 2 additions & 0 deletions WeaselIPCServer/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@

#include <wtl/atlapp.h>
#include <wtl/atlframe.h>

#include <boost/thread.hpp>
1 change: 0 additions & 1 deletion WeaselServer/WeaselServer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,6 @@
<ClCompile Include="WeaselTrayIcon.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="SystemTraySDK.h" />
<ClInclude Include="WeaselServerApp.h" />
Expand Down
3 changes: 0 additions & 3 deletions WeaselServer/WeaselServer.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="stdafx.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
4 changes: 2 additions & 2 deletions WeaselServer/WeaselTrayIcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "WeaselTrayIcon.h"

// nasty
#include "../WeaselServer/resource.h"
#include <resource.h>

static UINT mode_icon[] = { IDI_ZH, IDI_ZH, IDI_EN, IDI_RELOAD };
static const WCHAR *mode_label[] = { NULL, /*L"中文"*/ NULL, /*L"西文"*/ NULL, L"維護中" };
Expand Down Expand Up @@ -36,7 +36,7 @@ BOOL WeaselTrayIcon::Create(HWND hTargetWnd)

void WeaselTrayIcon::Refresh()
{
if (!m_style.display_tray_icon)
if (!m_style.display_tray_icon && !m_status.disabled) // display notification when deploying
{
if (m_mode != INITIAL)
{
Expand Down
31 changes: 0 additions & 31 deletions WeaselServer/resource.h

This file was deleted.

1 change: 1 addition & 0 deletions WeaselTSF/EditSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ STDAPI WeaselTSF::DoEditSession(TfEditCookie ec)
bool ok = m_client.GetResponseData(std::ref(parser));

_UpdateUI(*context, status);
_UpdateLanguageBar(status);

if (ok)
{
Expand Down
47 changes: 30 additions & 17 deletions WeaselTSF/LanguageBar.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "stdafx.h"
#include "resource.h"
#include <resource.h>
#include "WeaselTSF.h"

static const DWORD LANGBARITEMSINK_COOKIE = 0x42424242;
Expand Down Expand Up @@ -59,11 +59,14 @@ class CLangBarItemButton: public ITfLangBarItemButton, public ITfSource
STDMETHODIMP AdviseSink(REFIID riid, IUnknown *punk, DWORD *pdwCookie);
STDMETHODIMP UnadviseSink(DWORD dwCookie);

void UpdateStatus(weasel::Status stat);

private:
GUID _guid;
WeaselTSF *_pTextService;
ITfLangBarItemSink *_pLangBarItemSink;
LONG _cRef; /* COM Reference count */
bool ascii_mode;
};

CLangBarItemButton::CLangBarItemButton(WeaselTSF *pTextService, REFGUID guid)
Expand All @@ -74,6 +77,7 @@ CLangBarItemButton::CLangBarItemButton(WeaselTSF *pTextService, REFGUID guid)
_cRef = 1;
_pTextService = pTextService;
_guid = guid;
ascii_mode = false;
}

CLangBarItemButton::~CLangBarItemButton()
Expand Down Expand Up @@ -145,7 +149,11 @@ STDAPI CLangBarItemButton::OnClick(TfLBIClick click, POINT pt, const RECT *prcAr
{
if (click == TF_LBI_CLK_LEFT)
{
/* TODO : Switch mode */
_pTextService->_HandleLangBarMenuSelect(ascii_mode ? ID_WEASELTRAY_DISABLE_ASCII : ID_WEASELTRAY_ENABLE_ASCII);
ascii_mode = !ascii_mode;
if (_pLangBarItemSink) {
_pLangBarItemSink->OnUpdate(TF_LBI_STATUS | TF_LBI_ICON);
}
}
else if (click == TF_LBI_CLK_RIGHT)
{
Expand Down Expand Up @@ -180,7 +188,7 @@ STDAPI CLangBarItemButton::OnMenuSelect(UINT wID)

STDAPI CLangBarItemButton::GetIcon(HICON *phIcon)
{
*phIcon = (HICON) LoadImageW(g_hInst, MAKEINTRESOURCEW(IDI_ZH), IMAGE_ICON, 16, 16, LR_SHARED);
*phIcon = (HICON) LoadImageW(g_hInst, MAKEINTRESOURCEW(ascii_mode ? IDI_EN : IDI_ZH), IMAGE_ICON, 16, 16, LR_SHARED);
return (*phIcon == NULL)? E_FAIL: S_OK;
}

Expand Down Expand Up @@ -215,24 +223,23 @@ STDAPI CLangBarItemButton::UnadviseSink(DWORD dwCookie)
return S_OK;
}

void WeaselTSF::_HandleLangBarMenuSelect(UINT wID)
void CLangBarItemButton::UpdateStatus(weasel::Status stat)
{
switch (wID)
{
case ID_WEASELTRAY_QUIT:
case ID_WEASELTRAY_DEPLOY:
case ID_WEASELTRAY_SETTINGS:
case ID_WEASELTRAY_DICT_MANAGEMENT:
case ID_WEASELTRAY_WIKI:
case ID_WEASELTRAY_FORUM:
case ID_WEASELTRAY_CHECKUPDATE:
case ID_WEASELTRAY_INSTALLDIR:
case ID_WEASELTRAY_USERCONFIG:
/* TODO */
break;
if (stat.ascii_mode != ascii_mode) {
ascii_mode = stat.ascii_mode;
if (_pLangBarItemSink) {
_pLangBarItemSink->OnUpdate(TF_LBI_STATUS | TF_LBI_ICON);
}
}
}



void WeaselTSF::_HandleLangBarMenuSelect(UINT wID)
{
m_client.TrayCommand(wID);
}

HWND WeaselTSF::_GetFocusedContextWindow()
{
HWND hwnd = NULL;
Expand Down Expand Up @@ -304,3 +311,9 @@ void WeaselTSF::_UninitLanguageBar()
_pLangBarButton->Release();
_pLangBarButton = NULL;
}

void WeaselTSF::_UpdateLanguageBar(weasel::Status stat)
{
if (!_pLangBarButton) return;
_pLangBarButton->UpdateStatus(stat);
}
14 changes: 11 additions & 3 deletions WeaselTSF/WeaselTSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "WeaselTSF.h"
#include "WeaselCommon.h"
#include "CandidateList.h"
#include "ResponseParser.h"

static void error_message(const WCHAR *msg)
{
Expand Down Expand Up @@ -128,7 +129,6 @@ STDAPI WeaselTSF::Deactivate()
STDAPI WeaselTSF::ActivateEx(ITfThreadMgr *pThreadMgr, TfClientId tfClientId, DWORD dwFlags)
{
_activateFlags = dwFlags;
_EnsureServerConnected();

_pThreadMgr = pThreadMgr;
_pThreadMgr->AddRef();
Expand All @@ -152,12 +152,14 @@ STDAPI WeaselTSF::ActivateEx(ITfThreadMgr *pThreadMgr, TfClientId tfClientId, DW

// TODO not yet complete
_pLangBarButton = NULL;
//if (!_InitLanguageBar())
// goto ExitError;
if (!_InitLanguageBar())
goto ExitError;

if (!_IsKeyboardOpen())
_SetKeyboardOpen(TRUE);

_EnsureServerConnected();

return S_OK;

ExitError:
Expand All @@ -183,5 +185,11 @@ void WeaselTSF::_EnsureServerConnected()
m_client.Disconnect();
m_client.Connect(NULL);
m_client.StartSession();
weasel::Status status;
weasel::ResponseParser parser(NULL, NULL, &status, NULL, &_cand->style());
bool ok = m_client.GetResponseData(std::ref(parser));
if (ok) {
_UpdateLanguageBar(status);
}
}
}
4 changes: 3 additions & 1 deletion WeaselTSF/WeaselTSF.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace weasel {
class CandidateList;
}
class CLangBarItemButton;

class WeaselTSF:
public ITfTextInputProcessorEx,
Expand Down Expand Up @@ -109,6 +110,7 @@ class WeaselTSF:

BOOL _InitLanguageBar();
void _UninitLanguageBar();
void _UpdateLanguageBar(weasel::Status stat);

BOOL _InsertText(ITfContext *pContext, const std::wstring& ext);
void _AbortComposition(bool clear = true);
Expand All @@ -131,7 +133,7 @@ class WeaselTSF:

ITfComposition *_pComposition;

ITfLangBarItemButton *_pLangBarButton;
CLangBarItemButton *_pLangBarButton;

std::unique_ptr<weasel::CandidateList> _cand;

Expand Down
Binary file modified WeaselTSF/WeaselTSF.rc
Binary file not shown.
1 change: 0 additions & 1 deletion WeaselTSF/WeaselTSF.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@
<ClInclude Include="Globals.h" />
<ClInclude Include="KeyEvent.h" />
<ClInclude Include="Register.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="targetver.h" />
<ClInclude Include="WeaselTSF.h" />
Expand Down
Loading

0 comments on commit 2b66039

Please sign in to comment.