Skip to content

Commit

Permalink
feat(config): enable customization of label format
Browse files Browse the repository at this point in the history
Label text format can now be customized with style/label_format attribute.
  • Loading branch information
Prcuvu committed Mar 1, 2018
1 parent 6b686c7 commit 76b08ba
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 7 deletions.
5 changes: 5 additions & 0 deletions RimeWithWeasel/RimeWithWeasel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,11 @@ static void _UpdateUIStyle(RimeConfig* config, weasel::UI* ui, bool initialize)
style.layout_type = (style.layout_type == weasel::LAYOUT_HORIZONTAL)
? weasel::LAYOUT_HORIZONTAL_FULLSCREEN : weasel::LAYOUT_VERTICAL_FULLSCREEN;
}
char label_text_format[128] = { 0 };
if (RimeConfigGetString(config, "style/label_format", label_text_format, sizeof(label_text_format) - 1))
{
style.label_text_format = utf8towcs(label_text_format);
}
// layout (alternative to style/horizontal)
char layout_type[256] = {0};
if (RimeConfigGetString(config, "style/layout/type", layout_type, sizeof(layout_type) - 1))
Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/HorizontalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void HorizontalLayout::DoLayout(CDCHandle dc)
w += _style.candidate_spacing;

/* Label */
std::wstring label = GetLabelText(labels, i);
std::wstring label = GetLabelText(labels, i, _style.label_text_format.c_str());
dc.GetTextExtent(label.c_str(), label.length(), &size);
_candidateLabelRects[i].SetRect(w, height, w + size.cx, height + size.cy);
w += size.cx, h = max(h, size.cy);
Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/Layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace weasel
virtual CRect GetCandidateCommentRect(int id) const = 0;
virtual CRect GetStatusIconRect() const = 0;

virtual std::wstring GetLabelText(const std::vector<Text> &labels, int id) const = 0;
virtual std::wstring GetLabelText(const std::vector<Text> &labels, int id, const wchar_t *format) const = 0;
virtual bool IsInlinePreedit() const = 0;
virtual bool ShouldDisplayStatusIcon() const = 0;

Expand Down
6 changes: 4 additions & 2 deletions WeaselUI/StandardLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ StandardLayout::StandardLayout(const UIStyle &style, const Context &context, con
{
}

std::wstring StandardLayout::GetLabelText(const std::vector<Text> &labels, int id) const
std::wstring StandardLayout::GetLabelText(const std::vector<Text> &labels, int id, const wchar_t *format) const
{
return labels.at(id).str + L'.';
wchar_t buffer[128];
swprintf_s<128>(buffer, format, labels.at(id).str.c_str());
return std::wstring(buffer);
}

CSize StandardLayout::GetPreeditSize(CDCHandle dc) const
Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/StandardLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace weasel
virtual CRect GetCandidateCommentRect(int id) const { return _candidateCommentRects[id]; }
virtual CRect GetStatusIconRect() const { return _statusIconRect; }

virtual std::wstring GetLabelText(const std::vector<Text> &labels, int id) const;
virtual std::wstring GetLabelText(const std::vector<Text> &labels, int id, const wchar_t *format) const;
virtual bool IsInlinePreedit() const;
virtual bool ShouldDisplayStatusIcon() const;

Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/VerticalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void VerticalLayout::DoLayout(CDCHandle dc)
int w = _style.margin_x, h = 0;
int candidate_width = 0, comment_width = 0;
/* Label */
std::wstring label = GetLabelText(labels, i);
std::wstring label = GetLabelText(labels, i, _style.label_text_format.c_str());
dc.GetTextExtent(label.c_str(), label.length(), &size);
_candidateLabelRects[i].SetRect(w, height, w + size.cx, height + size.cy);
w += size.cx + space, h = max(h, size.cy);
Expand Down
2 changes: 1 addition & 1 deletion WeaselUI/WeaselPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ bool WeaselPanel::_DrawCandidates(CDCHandle dc)
dc.SetTextColor(m_style.label_text_color);

// Draw label
std::wstring label = m_layout->GetLabelText(labels, i);
std::wstring label = m_layout->GetLabelText(labels, i, m_style.label_text_format.c_str());
rect = m_layout->GetCandidateLabelRect(i);
_TextOut(dc, rect.left, rect.top, rect, label.c_str(), label.length());

Expand Down
2 changes: 2 additions & 0 deletions include/WeaselUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace weasel
bool inline_preedit;
PreeditType preedit_type;
bool display_tray_icon;
std::wstring label_text_format;
// layout
LayoutType layout_type;
int min_width;
Expand Down Expand Up @@ -64,6 +65,7 @@ namespace weasel
inline_preedit(false),
preedit_type(COMPOSITION),
display_tray_icon(false),
label_text_format(),
layout_type(LAYOUT_VERTICAL),
min_width(0),
min_height(0),
Expand Down
1 change: 1 addition & 0 deletions output/data/weasel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ style:
inline_preedit: false
preedit_type: composition
display_tray_icon: false
label_format: "%s."
layout:
min_width: 160
min_height: 0
Expand Down

0 comments on commit 76b08ba

Please sign in to comment.