Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

chore(windows): stop the notes panel burning 3.5 cores while listening

wquguru6eefe6b8c801
diff --git a/windows/src/Cicada/MainWindow.cs b/windows/src/Cicada/MainWindow.cs
index 0084513..7e35850 100644
--- a/windows/src/Cicada/MainWindow.cs
+++ b/windows/src/Cicada/MainWindow.cs
@@ -114,14 +114,13 @@ sealed class MainWindow : Window
}
list.Children.Add(Item(s));
}
- if (all.Count == 0) { selected = null; detail.Content = Empty(L("还没有会议记录"), L("点任务栏里的知了图标,开始听第一场会。")); return; }
- if (shown.Count == 0) { detail.Content = Empty(L("没有找到"), ""); return; }
+ // An empty page replaces the meeting, so the next real one must draw again.
+ if (all.Count == 0) { selected = null; shownKey = null; detail.Content = Empty(L("还没有会议记录"), L("点任务栏里的知了图标,开始听第一场会。")); return; }
+ if (shown.Count == 0) { shownKey = null; detail.Content = Empty(L("没有找到"), ""); return; }
if (selected is null || shown.All(s => s.Id != selected)) selected = shown[0].Id;
foreach (var child in list.Children.OfType<Button>()) Paint(child);
// Store changes come in a burst while a meeting runs; redraw the open page only if it changed.
- var key = app.Store.Get(selected!) is { } cur ? $"{selected}|{SessionStore.DisplayTitle(cur)}|{cur.Lines.Count}|{cur.Notes?.Covered}|{cur.Notes?.Final}|{app.Listen.Listening}" : null;
- if (key == shownKey) return;
- shownKey = key;
+ if (PageKey() == shownKey) return;
ShowDetail();
}
@@ -174,8 +173,14 @@ sealed class MainWindow : Window
return s;
}
+ /// What the open page shows; a store change that leaves it the same doesn't redraw it.
+ string? PageKey() => selected is not null && app.Store.Get(selected) is { } cur
+ ? $"{selected}|{tab}|{SessionStore.DisplayTitle(cur)}|{cur.Lines.Count}|{cur.Notes?.Covered}|{cur.Notes?.Final}|{app.Listen.Listening}"
+ : null;
+
void ShowDetail()
{
+ shownKey = PageKey();
if (selected is null || app.Store.Get(selected) is not { } s) { detail.Content = Empty(L("还没有会议记录"), ""); return; }
var live = app.Listen.Listening && app.Listen.SessionId == s.Id;
var top = new StackPanel { Margin = new Thickness(32, 24, 32, 0) };
diff --git a/windows/src/Cicada/Native.cs b/windows/src/Cicada/Native.cs
index adcf8e3..7f78b56 100644
--- a/windows/src/Cicada/Native.cs
+++ b/windows/src/Cicada/Native.cs
@@ -36,6 +36,25 @@ static class Native
});
}
+ [DllImport("dwmapi.dll")] static extern int DwmSetWindowAttribute(IntPtr hwnd, int attribute, ref int value, int size);
+ [DllImport("dwmapi.dll")] static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins margins);
+ [StructLayout(LayoutKind.Sequential)] struct Margins { public int Left, Right, Top, Bottom; }
+
+ /// A frameless window drawn by DWM: Windows 11's rounded corners and soft shadow, composited on
+ /// the GPU. (A transparent WPF window would have to draw its own shadow, in software, on every
+ /// change.) On Windows 10 the corners stay square.
+ public static void UseSystemFrame(Window window, bool dark = false)
+ {
+ var hwnd = new WindowInteropHelper(window).EnsureHandle();
+ int round = 2; // DWMWCP_ROUND
+ DwmSetWindowAttribute(hwnd, 33 /* DWMWA_WINDOW_CORNER_PREFERENCE */, ref round, sizeof(int));
+ int on = dark ? 1 : 0;
+ DwmSetWindowAttribute(hwnd, 20 /* DWMWA_USE_IMMERSIVE_DARK_MODE */, ref on, sizeof(int));
+ // A one-pixel frame margin is what gives a frameless window DWM's shadow.
+ var m = new Margins { Left = 0, Right = 0, Top = 1, Bottom = 0 };
+ DwmExtendFrameIntoClientArea(hwnd, ref m);
+ }
+
/// Out of screenshots and screen sharing (Windows 10 2004+), unless the user lets the other side see it.
public static bool SetCaptureVisible(Window window, bool visible)
{
diff --git a/windows/src/Cicada/NotesWindow.cs b/windows/src/Cicada/NotesWindow.cs
index ccf1740..4553cef 100644
--- a/windows/src/Cicada/NotesWindow.cs
+++ b/windows/src/Cicada/NotesWindow.cs
@@ -30,18 +30,23 @@ sealed class NotesWindow : Window
public NotesWindow(Controller app)
{
this.app = app;
+ // A plain frameless window, framed by DWM (UseSystemFrame), not a transparent one: those are
+ // redrawn in software, and with a shadow that cost most of a CPU core while listening.
WindowStyle = WindowStyle.None;
- AllowsTransparency = true;
- Background = Brushes.Transparent;
+ System.Windows.Shell.WindowChrome.SetWindowChrome(this, new System.Windows.Shell.WindowChrome
+ {
+ CaptionHeight = 0, ResizeBorderThickness = new Thickness(6), GlassFrameThickness = new Thickness(0, 1, 0, 0), UseAeroCaptionButtons = false,
+ });
Topmost = true;
ShowInTaskbar = false;
ShowActivated = false;
- ResizeMode = ResizeMode.CanResizeWithGrip;
+ ResizeMode = ResizeMode.CanResize;
FontFamily = Palette.Font;
Title = L("知了");
SourceInitialized += (_, _) =>
{
Native.MakeNonActivating(this);
+ Native.UseSystemFrame(this, Subtitles);
ApplyCaptureSetting();
};
// Drag from anywhere that isn't a control (buttons and links mark the press handled).
@@ -68,15 +73,15 @@ sealed class NotesWindow : Window
if (Subtitles)
{
SizeToContent = SizeToContent.Height; MinHeight = 80; MinWidth = 480;
- Width = Math.Min(920, area.Width - 40);
+ Width = Math.Min(900, area.Width - 48);
Left = area.Left + (area.Width - Width) / 2; Top = area.Bottom - Math.Max(ActualHeight, MinHeight) - 16;
placedSubtitles = ActualHeight > 0;
}
else
{
SizeToContent = SizeToContent.Manual; MinHeight = 360; MinWidth = 340;
- Width = 416; Height = Math.Min(836, area.Height - 16);
- Left = area.Right - Width - 8; Top = area.Top + 8;
+ Width = 400; Height = Math.Min(820, area.Height - 32);
+ Left = area.Right - Width - 16; Top = area.Top + 16;
}
}
@@ -119,6 +124,8 @@ sealed class NotesWindow : Window
builtFor = mode; headerKey = tabsKey = barsKey = null;
header = new ContentControl(); bars = new ContentControl(); live = new ContentControl(); tabs = new ContentControl(); footer = new ContentControl();
scroll = new ScrollViewer { VerticalScrollBarVisibility = ScrollBarVisibility.Auto, Padding = new Thickness(16, 14, 12, 16) };
+ Background = Subtitles ? Palette.Night : Palette.Surface;
+ if (IsLoaded) Native.UseSystemFrame(this, Subtitles);
Content = Subtitles ? SubtitleFrame() : PanelFrame();
}
var key = $"{app.Listen.Listening}|{app.Settings.ShowInCapture}|{hiddenFromCapture}";
@@ -153,12 +160,7 @@ sealed class NotesWindow : Window
DockPanel.SetDock(footer, Dock.Bottom);
dock.Children.Add(footer);
dock.Children.Add(scroll);
- return new Border
- {
- Background = Palette.Surface, BorderBrush = Palette.Stroke, BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(8),
- Child = dock, Margin = new Thickness(8),
- Effect = new System.Windows.Media.Effects.DropShadowEffect { BlurRadius = 24, ShadowDepth = 4, Opacity = 0.14 },
- };
+ return dock;
}
FrameworkElement Header()
@@ -368,13 +370,7 @@ sealed class NotesWindow : Window
grid.Children.Add(live);
grid.Children.Add(header);
var handle = new Border { Width = 36, Height = 4, CornerRadius = new CornerRadius(2), Background = new SolidColorBrush(Color.FromArgb(0x38, 0xFF, 0xFF, 0xFF)), HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, 0, 0, 6) };
- return new Border
- {
- Background = new SolidColorBrush(Color.FromArgb(0xEB, 0x15, 0x17, 0x1C)), BorderBrush = new SolidColorBrush(Color.FromArgb(0x14, 0xFF, 0xFF, 0xFF)),
- BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(12), Padding = new Thickness(16, 6, 10, 14), Margin = new Thickness(8),
- Child = new StackPanel { Children = { handle, grid } },
- Effect = new System.Windows.Media.Effects.DropShadowEffect { BlurRadius = 28, ShadowDepth = 6, Opacity = 0.35 },
- };
+ return new Border { Padding = new Thickness(16, 6, 10, 14), Child = new StackPanel { Children = { handle, grid } } };
}
FrameworkElement SubtitleLine()
diff --git a/windows/src/Cicada/Onboarding.cs b/windows/src/Cicada/Onboarding.cs
index 7ba4e4c..ae4f777 100644
--- a/windows/src/Cicada/Onboarding.cs
+++ b/windows/src/Cicada/Onboarding.cs
@@ -169,12 +169,19 @@ sealed class Onboarding : Window
if (!text.StartsWith("sk-")) { Status(false, L("要粘贴 sk- 开头的那串")); return; }
checking = true; footer.Content = Footer();
Status(null, L("正在检查…"));
+ var previous = app.Key.Read();
app.Key.Save(text);
app.Ling.KeyChanged();
var (state, _) = await app.Ling.Check();
checking = false; footer.Content = Footer();
- if (state == KeyState.Ok) { Status(true, L("可以用")); await Task.Delay(400); Go(1); }
- else Status(false, state == KeyState.Invalid ? L("这个 key 不能用,检查是否复制完整,或重新创建一个") : L("连不上 Ling,检查网络"));
+ if (state == KeyState.Ok) { Status(true, L("可以用")); await Task.Delay(400); Go(1); return; }
+ // A key Ling turned down isn't kept: the one that worked before, if any, comes back.
+ if (state == KeyState.Invalid)
+ {
+ if (previous is not null) app.Key.Save(previous); else app.Key.Remove();
+ app.Ling.KeyChanged();
+ }
+ Status(false, state == KeyState.Invalid ? L("这个 key 不能用,检查是否复制完整,或重新创建一个") : L("连不上 Ling,检查网络"));
}
// MARK: 2 · System audio
@@ -190,8 +197,10 @@ sealed class Onboarding : Window
{
double sum = 0;
foreach (var s in buf) sum += (double)s * s;
- level = buf.Length == 0 ? 0 : Math.Sqrt(sum / buf.Length) / 32768;
- peak = Math.Max(peak, level);
+ var rms = buf.Length == 0 ? 0 : Math.Sqrt(sum / buf.Length) / 32768;
+ // The loudest buffer since the last bar, not the last one (often a silent fill).
+ level = Math.Max(level, rms);
+ peak = Math.Max(peak, rms);
};
tap.Start();
meter.Start();
@@ -208,11 +217,13 @@ sealed class Onboarding : Window
/// 32 bars of the last few seconds of loudness, newest on the right.
void Meter()
{
+ // A rolling history of the loudness, eased so a word reads as a rise and fall, not one spike.
Array.Copy(levels, 1, levels, 0, levels.Length - 1);
- levels[^1] = Loudness(level);
+ levels[^1] = Math.Max(Loudness(level), levels[^2] * 0.7);
+ level = 0;
var bars = new StackPanel { Orientation = Orientation.Horizontal, Height = 40 };
foreach (var v in levels)
- bars.Children.Add(new Rectangle { Width = 12, Height = 6 + v * 34, RadiusX = 2, RadiusY = 2, Margin = new Thickness(0, 0, 3, 0), VerticalAlignment = VerticalAlignment.Center, Fill = v > 0.08 ? Palette.Live : Palette.Stroke });
+ bars.Children.Add(new Rectangle { Width = 12, Height = 4 + v * 36, RadiusX = 2, RadiusY = 2, Margin = new Thickness(0, 0, 3, 0), VerticalAlignment = VerticalAlignment.Center, Fill = v > 0.08 ? Palette.Live : Palette.Stroke });
System.Windows.Automation.AutomationProperties.SetName(bars, L("系统声音"));
meterHost.Content = bars;
var heard = peak >= 0.01;
@@ -247,6 +258,10 @@ sealed class Onboarding : Window
async Task PlaySample()
{
+ // The notes panel opens along the right edge; keep the wizard's buttons clear of it.
+ var area = SystemParameters.WorkArea;
+ var room = area.Right - 440 - area.Left;
+ Left = Math.Max(area.Left + 8, Math.Min(Left, room - Width));
playing = true; Render();
try { await app.PlaySample(); }
finally { playing = false; if (IsLoaded) Render(); }
diff --git a/windows/src/Cicada/TrayFlyout.cs b/windows/src/Cicada/TrayFlyout.cs
index 19afebc..0a01ad4 100644
--- a/windows/src/Cicada/TrayFlyout.cs
+++ b/windows/src/Cicada/TrayFlyout.cs
@@ -18,15 +18,15 @@ sealed class TrayFlyout : Window
public TrayFlyout(Controller app)
{
this.app = app;
- WindowStyle = WindowStyle.None; AllowsTransparency = true; Background = Brushes.Transparent;
+ // Framed by DWM like the notes panel: rounded, shadowed, and cheap to redraw each second.
+ WindowStyle = WindowStyle.None; Background = Palette.SurfaceAlt;
+ System.Windows.Shell.WindowChrome.SetWindowChrome(this, new System.Windows.Shell.WindowChrome { CaptionHeight = 0, ResizeBorderThickness = new Thickness(0), GlassFrameThickness = new Thickness(0, 1, 0, 0), UseAeroCaptionButtons = false });
+ SourceInitialized += (_, _) => Native.UseSystemFrame(this);
Topmost = true; ShowInTaskbar = false; ResizeMode = ResizeMode.NoResize;
- Width = 380 + 16; SizeToContent = SizeToContent.Height; FontFamily = Palette.Font; Title = L("知了");
- Content = new Border
- {
- Margin = new Thickness(8), Background = Palette.SurfaceAlt, BorderBrush = Palette.Stroke, BorderThickness = new Thickness(1), CornerRadius = new CornerRadius(8),
- Effect = new System.Windows.Media.Effects.DropShadowEffect { BlurRadius = 28, ShadowDepth = 6, Opacity = 0.2 }, Child = body,
- };
+ Width = 380; SizeToContent = SizeToContent.Height; FontFamily = Palette.Font; Title = L("知了");
+ Content = body;
Deactivated += (_, _) => { Hide(); hiddenAt = DateTime.UtcNow; };
+ KeyDown += (_, e) => { if (e.Key == System.Windows.Input.Key.Escape) Hide(); };
SizeChanged += (_, _) => Anchor();
clock.Tick += (_, _) => { if (app.Listen.Listening) Refresh(); };
IsVisibleChanged += (_, _) => { if (IsVisible) clock.Start(); else clock.Stop(); };
@@ -47,8 +47,8 @@ sealed class TrayFlyout : Window
void Anchor()
{
var area = SystemParameters.WorkArea;
- Left = area.Right - ActualWidth - 4;
- Top = area.Bottom - ActualHeight - 4;
+ Left = area.Right - ActualWidth - 12;
+ Top = area.Bottom - ActualHeight - 12;
}
public void Refresh() => body.Content = Build();
@@ -134,7 +134,7 @@ sealed class TrayFlyout : Window
records.MinWidth = 0; quit.MinWidth = 0;
var foot = Ui.Spread(records, quit);
foot.Margin = new Thickness(8, 6, 8, 6);
- stack.Children.Add(new Border { Background = Palette.Mica, BorderBrush = Palette.Stroke, BorderThickness = new Thickness(0, 1, 0, 0), CornerRadius = new CornerRadius(0, 0, 8, 8), Child = foot, Margin = new Thickness(0, recent.Count > 0 ? 0 : 16, 0, 0) });
+ stack.Children.Add(new Border { Background = Palette.Mica, BorderBrush = Palette.Stroke, BorderThickness = new Thickness(0, 1, 0, 0), Child = foot, Margin = new Thickness(0, recent.Count > 0 ? 0 : 16, 0, 0) });
return stack;
}
diff --git a/windows/src/Cicada/Ui.cs b/windows/src/Cicada/Ui.cs
index eee7562..f2d154c 100644
--- a/windows/src/Cicada/Ui.cs
+++ b/windows/src/Cicada/Ui.cs
@@ -225,15 +225,29 @@ static class Ui
public static FrameworkElement Dot(Brush color, double size = 8) => new Ellipse { Width = size, Height = size, Fill = color, VerticalAlignment = VerticalAlignment.Center };
- /// The live dot, breathing while listening.
+ /// The live dot, dimming and brightening once a second while listening. Not a WPF animation:
+ /// the notes panel and subtitle bar are transparent, shadowed windows that WPF redraws in
+ /// software, and an animation redraws them every frame (3.5 cores in a VM, starving the very
+ /// audio being listened to). One step a second costs one redraw a second.
public static FrameworkElement LiveDot(double size = 8)
{
var dot = new Ellipse { Width = size, Height = size, Fill = Palette.Live, VerticalAlignment = VerticalAlignment.Center };
- var pulse = new System.Windows.Media.Animation.DoubleAnimation(1, 0.35, TimeSpan.FromSeconds(0.9)) { AutoReverse = true, RepeatBehavior = System.Windows.Media.Animation.RepeatBehavior.Forever };
- dot.Loaded += (_, _) => dot.BeginAnimation(UIElement.OpacityProperty, pulse);
+ dot.Loaded += (_, _) => { liveDots.Add(dot); blink.Start(); };
+ dot.Unloaded += (_, _) => { liveDots.Remove(dot); if (liveDots.Count == 0) blink.Stop(); };
return dot;
}
+ static readonly HashSet<Ellipse> liveDots = [];
+ static bool dim;
+ static readonly System.Windows.Threading.DispatcherTimer blink = MakeBlink();
+
+ static System.Windows.Threading.DispatcherTimer MakeBlink()
+ {
+ var t = new System.Windows.Threading.DispatcherTimer { Interval = TimeSpan.FromSeconds(1) };
+ t.Tick += (_, _) => { dim = !dim; foreach (var d in liveDots) d.Opacity = dim ? 0.4 : 1; };
+ return t;
+ }
+
/// A small pill: 对方可见 and the like.
public static Border Badge(string text, Brush? bg = null, Brush? fg = null) => new()
{