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) {