fix issue with too large sentence and topmost window blocking dictionary

This commit is contained in:
Akash Mozumdar 2020-03-02 23:48:29 -07:00
parent fcb525df36
commit 6a61342d19
2 changed files with 9 additions and 4 deletions

View File

@ -16,6 +16,7 @@
#include <QWheelEvent> #include <QWheelEvent>
extern const char* EXTRA_WINDOW_INFO; extern const char* EXTRA_WINDOW_INFO;
extern const char* SENTENCE_TOO_BIG;
extern const char* TOPMOST; extern const char* TOPMOST;
extern const char* OPACITY; extern const char* OPACITY;
extern const char* SHOW_ORIGINAL; extern const char* SHOW_ORIGINAL;
@ -189,6 +190,7 @@ public:
void AddSentence(QString sentence) void AddSentence(QString sentence)
{ {
if (sentence.size() > maxSentenceSize) sentence = SENTENCE_TOO_BIG;
if (!showOriginal) sentence = sentence.section('\n', sentence.count('\n') / 2 + 1); if (!showOriginal) sentence = sentence.section('\n', sentence.count('\n') / 2 + 1);
sanitize(sentence); sanitize(sentence);
sentence.chop(std::distance(std::remove(sentence.begin(), sentence.end(), QChar::Tabulation), sentence.end())); sentence.chop(std::distance(std::remove(sentence.begin(), sentence.end(), QChar::Tabulation), sentence.end()));
@ -200,7 +202,8 @@ public:
private: private:
void setTopmost(bool topmost) void setTopmost(bool topmost)
{ {
SetWindowPos((HWND)winId(), topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); for (auto window : { winId(), dictionaryWindow.winId() })
SetWindowPos((HWND)window, topmost ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
settings.setValue(TOPMOST, topmost); settings.setValue(TOPMOST, topmost);
}; };
@ -232,10 +235,10 @@ private:
void computeDictionaryPosition(QPoint mouse) void computeDictionaryPosition(QPoint mouse)
{ {
QString sentence = ui.display->text();
const QFont& font = ui.display->font(); const QFont& font = ui.display->font();
if (cachedDisplayInfo.compareExchange(ui.display)) if (cachedDisplayInfo.compareExchange(ui.display))
{ {
QString sentence = ui.display->text();
QFontMetrics fontMetrics(font, ui.display); QFontMetrics fontMetrics(font, ui.display);
textPositionMap.clear(); textPositionMap.clear();
for (int i = 0, height = 0, lineBreak = 0; i < sentence.size(); ++i) for (int i = 0, height = 0, lineBreak = 0; i < sentence.size(); ++i)
@ -258,9 +261,9 @@ private:
int i; int i;
for (i = 0; i < textPositionMap.size(); ++i) if (textPositionMap[i].y() > mouse.y() && textPositionMap[i].x() > mouse.x()) break; for (i = 0; i < textPositionMap.size(); ++i) if (textPositionMap[i].y() > mouse.y() && textPositionMap[i].x() > mouse.x()) break;
if (i == textPositionMap.size() || (mouse - textPositionMap[i]).manhattanLength() > font.pointSize() * 2) return dictionaryWindow.hide(); if (i == textPositionMap.size() || (mouse - textPositionMap[i]).manhattanLength() > font.pointSize() * 2) return dictionaryWindow.hide();
if (ui.display->text().mid(i) == dictionaryWindow.term) return dictionaryWindow.ShowDefinition(); if (sentence.mid(i) == dictionaryWindow.term) return dictionaryWindow.ShowDefinition();
dictionaryWindow.ui.display->setFixedWidth(ui.display->width() * 3 / 4); dictionaryWindow.ui.display->setFixedWidth(ui.display->width() * 3 / 4);
dictionaryWindow.setTerm(ui.display->text().mid(i)); dictionaryWindow.setTerm(sentence.mid(i));
int left = i == 0 ? 0 : textPositionMap[i - 1].x(), right = textPositionMap[i].x(), int left = i == 0 ? 0 : textPositionMap[i - 1].x(), right = textPositionMap[i].x(),
x = textPositionMap[i].x() > ui.display->width() / 2 ? -dictionaryWindow.width() + (right * 3 + left) / 4 : (left * 3 + right) / 4; x = textPositionMap[i].x() > ui.display->width() / 2 ? -dictionaryWindow.width() + (right * 3 + left) / 4 : (left * 3 + right) / 4;
dictionaryWindow.move(ui.display->mapToGlobal(QPoint(x, textPositionMap[i].y()))); dictionaryWindow.move(ui.display->mapToGlobal(QPoint(x, textPositionMap[i].y())));
@ -293,6 +296,7 @@ private:
} }
bool locked, showOriginal, useDictionary; bool locked, showOriginal, useDictionary;
int maxSentenceSize = 1500;
QPoint oldPos; QPoint oldPos;
class class

View File

@ -122,6 +122,7 @@ const wchar_t* TOO_MANY_TRANS_REQUESTS = L"Too many translation requests: refuse
const wchar_t* TRANSLATION_ERROR = L"Error while translating"; const wchar_t* TRANSLATION_ERROR = L"Error while translating";
const char* EXTRA_WINDOW_INFO = u8R"(Right click to change settings const char* EXTRA_WINDOW_INFO = u8R"(Right click to change settings
Click and drag on window edges to move, or the bottom right corner to resize)"; Click and drag on window edges to move, or the bottom right corner to resize)";
const char* SENTENCE_TOO_BIG = u8"Sentence much too large to display";
const char* TOPMOST = u8"Always on top"; const char* TOPMOST = u8"Always on top";
const char* DICTIONARY = u8"Dictionary"; const char* DICTIONARY = u8"Dictionary";
const char* DICTIONARY_INSTRUCTIONS = u8R"(This file is used only for the "Dictionary" feature of the Extra Window extension. const char* DICTIONARY_INSTRUCTIONS = u8R"(This file is used only for the "Dictionary" feature of the Extra Window extension.