diff --git a/crates/alkane-gui/src/main.rs b/crates/alkane-gui/src/main.rs index cfae3642..574867a6 100644 --- a/crates/alkane-gui/src/main.rs +++ b/crates/alkane-gui/src/main.rs @@ -99,6 +99,17 @@ fn run_selftest() -> i32 { let frame = wx_rs::Widget::main_frame(); let mut app = ui::build(frame); let ok = app.run_fixture_selftest(); + // ALKANE_SNAPSHOT=path (macOS): also write a PNG of the window as the app + // draws it, to check rendering without screen-recording permission. + if let Some(path) = std::env::var_os("ALKANE_SNAPSHOT") { + let path = path.to_string_lossy(); + let wrote = wx_rs::snapshot_png(&frame, &path); + println!( + "SNAPSHOT {} {}", + if wrote { "wrote" } else { "unavailable" }, + path + ); + } println!("SELFTEST {}", if ok { "PASS" } else { "FAIL" }); if ok { 0 diff --git a/thirdparty/wx-rs/patches/0010-macos-table-render.patch b/thirdparty/wx-rs/patches/0010-macos-table-render.patch new file mode 100644 index 00000000..e0e0fbc7 --- /dev/null +++ b/thirdparty/wx-rs/patches/0010-macos-table-render.patch @@ -0,0 +1,162 @@ +Subject: [PATCH 0010] macOS: native alternating rows, theme text colour, self-test snapshot + +--- a/build.rs ++++ b/build.rs +@@ -278,6 +278,12 @@ + build.cpp(true) + .file("cpp_src/wxbridge.cpp") + .file("cpp_src/widgets_bridge.cpp"); ++ if is_osx() { ++ // Cocoa calls wx does not wrap (native alternating rows, self-test ++ // snapshot); clang compiles .mm as Objective-C++. ++ println!("cargo:rerun-if-changed=cpp_src/osx_native.mm"); ++ build.file("cpp_src/osx_native.mm"); ++ } + let mut flags = cxxflags.split_whitespace(); + while let Some(f) = flags.next() { + // A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds +--- a/cpp_src/widgets_bridge.cpp ++++ b/cpp_src/widgets_bridge.cpp +@@ -597,7 +597,11 @@ + } else if (GetAttr().HasBackgroundColour()) { + cellbg = GetAttr().GetBackgroundColour(); + } else { ++#ifdef __WXOSX__ ++ cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX); ++#else + cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); ++#endif + } + x += DrawShape(dc, shape, x, cell, seg.has_colour ? seg.colour : plain, cellbg); + continue; +@@ -688,6 +692,11 @@ + (unsigned char)(c.blue * 255.0 + 0.5)); + } + } ++#elif defined(__WXOSX__) ++ // The DC arrives with the control's foreground colour, which is not the ++ // theme-aware label colour native cells use; take the list text colour so ++ // uncoloured segments follow light/dark like the native columns. ++ return wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT); + #endif + return wxColour(); + } +@@ -1463,8 +1472,21 @@ + + // Set (or clear, when r<0) the alternating-row (zebra) background colour. The + // app passes a theme-aware colour; odd rows get it (see AttrListStore). ++#ifdef __WXOSX__ ++extern "C" void wxbr_osx_set_alternating_rows(void* win, int on); ++#endif + void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) { ++#ifdef __WXOSX__ ++ // wxOSX draws custom cells without clipping them to the cell, so the ++ // renderer's over-sized background fill (which GTK/MSW clip) painted over ++ // neighbouring cells and hid their text until a row was selected. Use the ++ // table's native alternating rows instead: AppKit paints them under every ++ // cell, in the theme's colours, and no row carries a background attribute. ++ dvlc_store(win)->zebra = wxColour(); ++ wxbr_osx_set_alternating_rows(win, r >= 0 ? 1 : 0); ++#else + dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b); ++#endif + } + + // The model-row index of the first (top) visible row, or -1 if none. Used to +--- a/cpp_src/osx_native.mm ++++ b/cpp_src/osx_native.mm +@@ -0,0 +1,60 @@ ++// osx_native.mm -- alkane-toolkit extension: the few wxOSX/Cocoa calls the ++// bridge needs that wx does not wrap. Compiled on macOS only (see build.rs). ++ ++#include ++#include ++ ++#import ++ ++// The NSTableView (NSOutlineView) behind a wxDataViewCtrl. wx's handle is the ++// enclosing NSScrollView. ++static NSTableView* TableOf(wxWindow* win) { ++ if (!win) return nil; ++ id h = (id)win->GetHandle(); ++ if ([h isKindOfClass:[NSTableView class]]) return (NSTableView*)h; ++ if ([h isKindOfClass:[NSScrollView class]]) { ++ id doc = [(NSScrollView*)h documentView]; ++ if ([doc isKindOfClass:[NSTableView class]]) return (NSTableView*)doc; ++ } ++ return nil; ++} ++ ++extern "C" { ++ ++// Native alternating-row backgrounds. AppKit paints them under every cell, ++// custom-drawn ones included, in the theme's own colours (light and dark), so ++// custom renderers never paint row backgrounds on macOS. ++void wxbr_osx_set_alternating_rows(void* win, int on) { ++ NSTableView* tv = TableOf((wxWindow*)win); ++ if (tv) [tv setUsesAlternatingRowBackgroundColors:(on ? YES : NO)]; ++} ++ ++// Write a PNG of a top-level window's content, rendered by the app itself ++// (no screen-recording permission needed). Used by the self-test with ++// ALKANE_SNAPSHOT=path to check what the table really draws. Returns 1 on ++// success. ++int wxbr_osx_snapshot(void* win, const char* path) { ++ wxWindow* w = (wxWindow*)win; ++ if (!w || !path) return 0; ++ wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(w), wxTopLevelWindow); ++ if (!tlw) return 0; ++ tlw->Show(); ++ tlw->Raise(); ++ // Let the window lay out and paint before capturing it. ++ for (int i = 0; i < 30; ++i) { ++ wxYield(); ++ wxMilliSleep(30); ++ } ++ NSWindow* nsw = (NSWindow*)tlw->GetWXWindow(); ++ NSView* view = [nsw contentView]; ++ if (!view) return 0; ++ NSRect rect = [view bounds]; ++ NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect]; ++ if (!rep) return 0; ++ [view cacheDisplayInRect:rect toBitmapImageRep:rep]; ++ NSData* png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}]; ++ if (!png) return 0; ++ return [png writeToFile:[NSString stringWithUTF8String:path] atomically:YES] ? 1 : 0; ++} ++ ++} +--- a/src/widgets.rs ++++ b/src/widgets.rs +@@ -107,6 +107,8 @@ + pub fn wxbr_launch_browser(url: *const c_char) -> c_int; + pub fn wxbr_set_appearance_system(); + pub fn wxbr_is_dark() -> c_int; ++ #[cfg(target_os = "macos")] ++ pub fn wxbr_osx_snapshot(win: *mut c_void, path: *const c_char) -> c_int; + pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8); + pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int); + pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int); +@@ -271,6 +273,22 @@ + unsafe { notsafe::wxbr_is_dark() != 0 } + } + ++/// Write a PNG of `win`'s top-level window as the app itself renders it (shows ++/// the window and lets it paint first). macOS only, for the self-test's ++/// `ALKANE_SNAPSHOT`; returns false elsewhere or on failure. ++pub fn snapshot_png(win: &Widget, path: &str) -> bool { ++ #[cfg(target_os = "macos")] ++ { ++ let Ok(p) = CString::new(path) else { return false }; ++ unsafe { notsafe::wxbr_osx_snapshot(win.0, p.as_ptr()) != 0 } ++ } ++ #[cfg(not(target_os = "macos"))] ++ { ++ let _ = (win, path); ++ false ++ } ++} ++ + /// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight, + /// 3=button face, 4=grey text. Returns (r, g, b). + pub fn system_color(which: i32) -> (u8, u8, u8) { diff --git a/thirdparty/wx-rs/patches/README.md b/thirdparty/wx-rs/patches/README.md index 45db9776..9c2cf13a 100644 --- a/thirdparty/wx-rs/patches/README.md +++ b/thirdparty/wx-rs/patches/README.md @@ -28,6 +28,7 @@ later changes go in new patches. | 0007 | `wxbridge.cpp` on wxMSW: no input-swallowing overlay, appearance before the first window, work-area sizing, executable icon, UTF-8 menu fix | | 0008 | Widget bridge on wxMSW: generic data-view input and tooltips, DIP boundary, anti-aliased shapes, themed selection colours | | 0009 | macOS: wxOSX (universal static) through `wx-config`, logical sizes on Apple, clang compiler runtime for `@available` | +| 0010 | macOS table: native alternating rows instead of the unclipped custom-cell background fill (it hid neighbouring cells' text), theme-aware text colour, `osx_native.mm`, self-test window snapshot | History: the round 19 health-tooltip fix (GTK tip area converted from bin-window to widget coordinates) used to be a separate patch on top of an diff --git a/thirdparty/wx-rs/wx-rs-0.2.0-patched/build.rs b/thirdparty/wx-rs/wx-rs-0.2.0-patched/build.rs index 48628b20..8b2036b8 100644 --- a/thirdparty/wx-rs/wx-rs-0.2.0-patched/build.rs +++ b/thirdparty/wx-rs/wx-rs-0.2.0-patched/build.rs @@ -278,6 +278,12 @@ fn build_with_wx_config() -> Result<(), Box> { build.cpp(true) .file("cpp_src/wxbridge.cpp") .file("cpp_src/widgets_bridge.cpp"); + if is_osx() { + // Cocoa calls wx does not wrap (native alternating rows, self-test + // snapshot); clang compiles .mm as Objective-C++. + println!("cargo:rerun-if-changed=cpp_src/osx_native.mm"); + build.file("cpp_src/osx_native.mm"); + } let mut flags = cxxflags.split_whitespace(); while let Some(f) = flags.next() { // A universal wx prefix reports `-arch arm64 -arch x86_64`; cargo builds diff --git a/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/osx_native.mm b/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/osx_native.mm new file mode 100644 index 00000000..cf8e3c62 --- /dev/null +++ b/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/osx_native.mm @@ -0,0 +1,60 @@ +// osx_native.mm -- alkane-toolkit extension: the few wxOSX/Cocoa calls the +// bridge needs that wx does not wrap. Compiled on macOS only (see build.rs). + +#include +#include + +#import + +// The NSTableView (NSOutlineView) behind a wxDataViewCtrl. wx's handle is the +// enclosing NSScrollView. +static NSTableView* TableOf(wxWindow* win) { + if (!win) return nil; + id h = (id)win->GetHandle(); + if ([h isKindOfClass:[NSTableView class]]) return (NSTableView*)h; + if ([h isKindOfClass:[NSScrollView class]]) { + id doc = [(NSScrollView*)h documentView]; + if ([doc isKindOfClass:[NSTableView class]]) return (NSTableView*)doc; + } + return nil; +} + +extern "C" { + +// Native alternating-row backgrounds. AppKit paints them under every cell, +// custom-drawn ones included, in the theme's own colours (light and dark), so +// custom renderers never paint row backgrounds on macOS. +void wxbr_osx_set_alternating_rows(void* win, int on) { + NSTableView* tv = TableOf((wxWindow*)win); + if (tv) [tv setUsesAlternatingRowBackgroundColors:(on ? YES : NO)]; +} + +// Write a PNG of a top-level window's content, rendered by the app itself +// (no screen-recording permission needed). Used by the self-test with +// ALKANE_SNAPSHOT=path to check what the table really draws. Returns 1 on +// success. +int wxbr_osx_snapshot(void* win, const char* path) { + wxWindow* w = (wxWindow*)win; + if (!w || !path) return 0; + wxTopLevelWindow* tlw = wxDynamicCast(wxGetTopLevelParent(w), wxTopLevelWindow); + if (!tlw) return 0; + tlw->Show(); + tlw->Raise(); + // Let the window lay out and paint before capturing it. + for (int i = 0; i < 30; ++i) { + wxYield(); + wxMilliSleep(30); + } + NSWindow* nsw = (NSWindow*)tlw->GetWXWindow(); + NSView* view = [nsw contentView]; + if (!view) return 0; + NSRect rect = [view bounds]; + NSBitmapImageRep* rep = [view bitmapImageRepForCachingDisplayInRect:rect]; + if (!rep) return 0; + [view cacheDisplayInRect:rect toBitmapImageRep:rep]; + NSData* png = [rep representationUsingType:NSBitmapImageFileTypePNG properties:@{}]; + if (!png) return 0; + return [png writeToFile:[NSString stringWithUTF8String:path] atomically:YES] ? 1 : 0; +} + +} diff --git a/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp b/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp index e83bcc2f..193ca2dd 100644 --- a/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp +++ b/thirdparty/wx-rs/wx-rs-0.2.0-patched/cpp_src/widgets_bridge.cpp @@ -597,7 +597,11 @@ class SegmentRenderer : public wxDataViewCustomRenderer { } else if (GetAttr().HasBackgroundColour()) { cellbg = GetAttr().GetBackgroundColour(); } else { +#ifdef __WXOSX__ + cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOX); +#else cellbg = wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW); +#endif } x += DrawShape(dc, shape, x, cell, seg.has_colour ? seg.colour : plain, cellbg); continue; @@ -688,6 +692,11 @@ class SegmentRenderer : public wxDataViewCustomRenderer { (unsigned char)(c.blue * 255.0 + 0.5)); } } +#elif defined(__WXOSX__) + // The DC arrives with the control's foreground colour, which is not the + // theme-aware label colour native cells use; take the list text colour so + // uncoloured segments follow light/dark like the native columns. + return wxSystemSettings::GetColour(wxSYS_COLOUR_LISTBOXTEXT); #endif return wxColour(); } @@ -1463,8 +1472,21 @@ void wxbr_dvlc_set_row_color(void* win, unsigned row, int r, int g, int b) { // Set (or clear, when r<0) the alternating-row (zebra) background colour. The // app passes a theme-aware colour; odd rows get it (see AttrListStore). +#ifdef __WXOSX__ +extern "C" void wxbr_osx_set_alternating_rows(void* win, int on); +#endif void wxbr_dvlc_set_zebra(void* win, int r, int g, int b) { +#ifdef __WXOSX__ + // wxOSX draws custom cells without clipping them to the cell, so the + // renderer's over-sized background fill (which GTK/MSW clip) painted over + // neighbouring cells and hid their text until a row was selected. Use the + // table's native alternating rows instead: AppKit paints them under every + // cell, in the theme's colours, and no row carries a background attribute. + dvlc_store(win)->zebra = wxColour(); + wxbr_osx_set_alternating_rows(win, r >= 0 ? 1 : 0); +#else dvlc_store(win)->zebra = (r < 0) ? wxColour() : wxColour(r, g, b); +#endif } // The model-row index of the first (top) visible row, or -1 if none. Used to diff --git a/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs b/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs index 023f0edb..eada9414 100644 --- a/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs +++ b/thirdparty/wx-rs/wx-rs-0.2.0-patched/src/widgets.rs @@ -107,6 +107,8 @@ mod notsafe { pub fn wxbr_launch_browser(url: *const c_char) -> c_int; pub fn wxbr_set_appearance_system(); pub fn wxbr_is_dark() -> c_int; + #[cfg(target_os = "macos")] + pub fn wxbr_osx_snapshot(win: *mut c_void, path: *const c_char) -> c_int; pub fn wxbr_system_color(which: c_int, r: *mut u8, g: *mut u8, b: *mut u8); pub fn wxbr_set_bg(win: *mut c_void, r: c_int, g: c_int, b: c_int); pub fn wxbr_set_fg(win: *mut c_void, r: c_int, g: c_int, b: c_int); @@ -271,6 +273,22 @@ pub fn is_dark() -> bool { unsafe { notsafe::wxbr_is_dark() != 0 } } +/// Write a PNG of `win`'s top-level window as the app itself renders it (shows +/// the window and lets it paint first). macOS only, for the self-test's +/// `ALKANE_SNAPSHOT`; returns false elsewhere or on failure. +pub fn snapshot_png(win: &Widget, path: &str) -> bool { + #[cfg(target_os = "macos")] + { + let Ok(p) = CString::new(path) else { return false }; + unsafe { notsafe::wxbr_osx_snapshot(win.0, p.as_ptr()) != 0 } + } + #[cfg(not(target_os = "macos"))] + { + let _ = (win, path); + false + } +} + /// A theme colour. `which`: 0=window bg, 1=window text, 2=highlight, /// 3=button face, 4=grey text. Returns (r, g, b). pub fn system_color(which: i32) -> (u8, u8, u8) {