From cd76702ab21bfe39c7b1804f63caf8994c435ced Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Thu, 14 Nov 2024 12:26:22 +0100
Subject: xA/xW: dehighlight current buffer appropriately
---
xA/xA.go | 148 ++++++++++++++++++++++++++++------------------------
xW/xW.cpp | 175 ++++++++++++++++++++++++++++++++++++--------------------------
2 files changed, 182 insertions(+), 141 deletions(-)
diff --git a/xA/xA.go b/xA/xA.go
index 2cf2796..17a0400 100644
--- a/xA/xA.go
+++ b/xA/xA.go
@@ -363,16 +363,18 @@ func bufferByName(name string) *buffer {
return nil
}
-func bufferActivate(name string) {
- relaySend(RelayCommandData{
- Variant: &RelayCommandDataBufferActivate{BufferName: name},
- }, nil)
+func bufferAtBottom() bool {
+ return wRichScroll.Offset.Y >=
+ wRichScroll.Content.Size().Height-wRichScroll.Size().Height
}
-func bufferToggleUnimportant(name string) {
- relaySend(RelayCommandData{
- Variant: &RelayCommandDataBufferToggleUnimportant{BufferName: name},
- }, nil)
+func bufferScrollToBottom() {
+ // XXX: Doing it once is not reliable, something's amiss.
+ // (In particular, nothing happens when we switch from an empty buffer
+ // to a buffer than needs scrolling.)
+ wRichScroll.ScrollToBottom()
+ wRichScroll.ScrollToBottom()
+ refreshStatus()
}
func bufferPushLine(b *buffer, line bufferLine) {
@@ -386,68 +388,20 @@ func bufferPushLine(b *buffer, line bufferLine) {
}
}
-// --- Current buffer ----------------------------------------------------------
-
-func bufferToggleLogFinish(err string, response *RelayResponseDataBufferLog) {
- if response == nil {
- showErrorMessage(err)
- return
- }
-
- wLog.SetText(string(response.Log))
- wLog.Show()
- wRichScroll.Hide()
-}
-
-func bufferToggleLog() {
- if wLog.Visible() {
- wRichScroll.Show()
- wLog.Hide()
- wLog.SetText("")
- return
- }
-
- name := bufferCurrent
- relaySend(RelayCommandData{Variant: &RelayCommandDataBufferLog{
- BufferName: name,
- }}, func(err string, response *RelayResponseData) {
- if bufferCurrent == name {
- bufferToggleLogFinish(
- err, response.Variant.(*RelayResponseDataBufferLog))
- }
- })
-}
-
-func bufferAtBottom() bool {
- return wRichScroll.Offset.Y >=
- wRichScroll.Content.Size().Height-wRichScroll.Size().Height
-}
-
-func bufferScrollToBottom() {
- // XXX: Doing it once is not reliable, something's amiss.
- // (In particular, nothing happens when we switch from an empty buffer
- // to a buffer than needs scrolling.)
- wRichScroll.ScrollToBottom()
- wRichScroll.ScrollToBottom()
- refreshStatus()
-}
-
// --- UI state refresh --------------------------------------------------------
func refreshIcon() {
- highlighted := false
+ resource := resourceIconNormal
for _, b := range buffers {
if b.highlighted {
- highlighted = true
+ resource = resourceIconHighlighted
break
}
}
- if highlighted {
- wWindow.SetIcon(resourceIconHighlighted)
- } else {
- wWindow.SetIcon(resourceIconNormal)
- }
+ // Prevent deadlocks (though it might have a race condition).
+ // https://github.com/fyne-io/fyne/issues/5266
+ go func() { wWindow.SetIcon(resource) }()
}
func refreshTopic(topic []bufferLineItem) {
@@ -515,6 +469,63 @@ func refreshStatus() {
wStatus.SetText(status)
}
+func recheckHighlighted() {
+ // Corresponds to the logic toggling the bool on.
+ if b := bufferByName(bufferCurrent); b != nil &&
+ b.highlighted && bufferAtBottom() &&
+ inForeground && !wLog.Visible() {
+ b.highlighted = false
+ refreshIcon()
+ refreshBufferList()
+ }
+}
+
+// --- Buffer actions ----------------------------------------------------------
+
+func bufferActivate(name string) {
+ relaySend(RelayCommandData{
+ Variant: &RelayCommandDataBufferActivate{BufferName: name},
+ }, nil)
+}
+
+func bufferToggleUnimportant(name string) {
+ relaySend(RelayCommandData{
+ Variant: &RelayCommandDataBufferToggleUnimportant{BufferName: name},
+ }, nil)
+}
+
+func bufferToggleLogFinish(err string, response *RelayResponseDataBufferLog) {
+ if response == nil {
+ showErrorMessage(err)
+ return
+ }
+
+ wLog.SetText(string(response.Log))
+ wLog.Show()
+ wRichScroll.Hide()
+}
+
+func bufferToggleLog() {
+ if wLog.Visible() {
+ wRichScroll.Show()
+ wLog.Hide()
+ wLog.SetText("")
+
+ recheckHighlighted()
+ return
+ }
+
+ name := bufferCurrent
+ relaySend(RelayCommandData{Variant: &RelayCommandDataBufferLog{
+ BufferName: name,
+ }}, func(err string, response *RelayResponseData) {
+ if bufferCurrent == name {
+ bufferToggleLogFinish(
+ err, response.Variant.(*RelayResponseDataBufferLog))
+ }
+ })
+}
+
// --- RichText formatting -----------------------------------------------------
func defaultBufferLineItem() bufferLineItem { return bufferLineItem{} }
@@ -756,6 +767,7 @@ func refreshBuffer(b *buffer) {
bufferPrintAndWatchTrailingDateChanges()
wRichText.Refresh()
bufferScrollToBottom()
+ recheckHighlighted()
}
// --- Event processing --------------------------------------------------------
@@ -1374,6 +1386,9 @@ func (l *customLayout) Layout(objects []fyne.CanvasObject, size fyne.Size) {
}
if toBottom {
bufferScrollToBottom()
+ } else {
+ recheckHighlighted()
+ refreshStatus()
}
}
@@ -1497,11 +1512,7 @@ func main() {
a.Lifecycle().SetOnEnteredForeground(func() {
// TODO(p): Does this need locking?
inForeground = true
- if b := bufferByName(bufferCurrent); b != nil {
- b.highlighted = false
- refreshIcon()
- refreshBufferList()
- }
+ recheckHighlighted()
})
a.Lifecycle().SetOnExitedForeground(func() {
inForeground = false
@@ -1542,7 +1553,10 @@ func main() {
wRichText = widget.NewRichText()
wRichText.Wrapping = fyne.TextWrapWord
wRichScroll = container.NewVScroll(wRichText)
- wRichScroll.OnScrolled = func(position fyne.Position) { refreshStatus() }
+ wRichScroll.OnScrolled = func(position fyne.Position) {
+ recheckHighlighted()
+ refreshStatus()
+ }
wLog = newLogEntry()
wLog.Wrapping = fyne.TextWrapWord
wLog.Hide()
diff --git a/xW/xW.cpp b/xW/xW.cpp
index 20ae32e..7fd8950 100644
--- a/xW/xW.cpp
+++ b/xW/xW.cpp
@@ -255,73 +255,6 @@ buffer_by_name(const std::wstring &name)
return nullptr;
}
-static void
-buffer_activate(const std::wstring &name)
-{
- auto activate = new Relay::CommandData_BufferActivate();
- activate->buffer_name = name;
- relay_send(activate);
-}
-
-static void
-buffer_toggle_unimportant(const std::wstring &name)
-{
- auto toggle = new Relay::CommandData_BufferToggleUnimportant();
- toggle->buffer_name = name;
- relay_send(toggle);
-}
-
-// --- Current buffer ----------------------------------------------------------
-
-static void
-buffer_toggle_log(
- const std::wstring &error, const Relay::ResponseData_BufferLog *response)
-{
- if (!response) {
- show_error_message(error.c_str());
- return;
- }
-
- std::wstring log;
- if (!LibertyXDR::utf8_to_wstring(
- response->log.data(), response->log.size(), log)) {
- show_error_message(L"Invalid encoding.");
- return;
- }
-
- std::wstring filtered;
- for (auto wch : log) {
- if (wch == L'\n')
- filtered += L"\r\n";
- else
- filtered += wch;
- }
-
- SetWindowText(g.hwndBufferLog, filtered.c_str());
- ShowWindow(g.hwndBuffer, SW_HIDE);
- ShowWindow(g.hwndBufferLog, SW_SHOW);
-}
-
-static void
-buffer_toggle_log()
-{
- if (IsWindowVisible(g.hwndBufferLog)) {
- ShowWindow(g.hwndBufferLog, SW_HIDE);
- ShowWindow(g.hwndBuffer, SW_SHOW);
- SetWindowText(g.hwndBufferLog, L"");
- return;
- }
-
- auto log = new Relay::CommandData_BufferLog();
- log->buffer_name = g.buffer_current;
- relay_send(log, [name = g.buffer_current](auto error, auto response) {
- if (g.buffer_current != name)
- return;
- buffer_toggle_log(error,
- dynamic_cast