Skip to content

Commit

Permalink
Allow configuring whether to display two triangles, or only one
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 30, 2024
1 parent bab7df5 commit 8848e79
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ display-link = []
# Disable VSync (i.e. use `wgpu::PresentMode::Immediate`).
no-vsync = []

# Show two triangles side by side instead of just one.
two-triangles = []

# Use in-development `wgpu` with breaking changes.
wgpu-unstable = []

Expand Down
32 changes: 16 additions & 16 deletions src/appkit_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use objc2::runtime::ProtocolObject;
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2_app_kit::{
NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate, NSBackingStoreType,
NSStackView, NSStackViewDistribution, NSUserInterfaceLayoutDirection,
NSUserInterfaceLayoutOrientation, NSWindow, NSWindowStyleMask,
NSStackView, NSStackViewDistribution, NSUserInterfaceLayoutOrientation, NSWindow,
NSWindowStyleMask,
};
use objc2_foundation::{
CGPoint, CGRect, CGSize, MainThreadMarker, NSNotification, NSObject, NSObjectProtocol, NSPoint,
Expand Down Expand Up @@ -84,20 +84,20 @@ impl Delegate {
// Important for memory safety!
unsafe { window.setReleasedWhenClosed(false) };

let frame = window.contentView().expect("window content view").frame();
unsafe {
let view = NSStackView::new(mtm);
view.addArrangedSubview(&WgpuTriangleView::new(
mtm,
CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(1.0, 1.0)),
));
let wgpu_view = WgpuTriangleView::new(
mtm,
CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(1.0, 1.0)),
);
view.addArrangedSubview(&wgpu_view);
view.setOrientation(NSUserInterfaceLayoutOrientation::Horizontal);
view.setDistribution(NSStackViewDistribution::FillEqually);
if cfg!(feature = "two-triangles") {
// Frame will be resized by NSStackView automatically
let frame = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(1.0, 1.0));
unsafe {
let view = NSStackView::new(mtm);
view.addArrangedSubview(&WgpuTriangleView::new(mtm, frame));
view.addArrangedSubview(&WgpuTriangleView::new(mtm, frame));
view.setOrientation(NSUserInterfaceLayoutOrientation::Horizontal);
view.setDistribution(NSStackViewDistribution::FillEqually);
window.setContentView(Some(&view));
}
} else {
let frame = window.contentView().expect("window content view").frame();
let view = WgpuTriangleView::new(mtm, frame);
window.setContentView(Some(&view));
}

Expand Down
27 changes: 13 additions & 14 deletions src/uikit_main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,19 @@ impl Delegate {

let view_controller = ViewController::new(mtm);

unsafe {
let view = UIStackView::new(mtm);
view.addArrangedSubview(&WgpuTriangleView::new(
mtm,
CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(1.0, 1.0)),
));
let wgpu_view = WgpuTriangleView::new(
mtm,
CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(1.0, 1.0)),
);
view.addArrangedSubview(&wgpu_view);
// view.setOrientation(NSUserInterfaceLayoutOrientation::Horizontal);
view.setDistribution(UIStackViewDistribution::FillEqually);
view_controller.setView(Some(&view));
if cfg!(feature = "two-triangles") {
// Frame will be resized by NSStackView automatically
let frame = CGRect::new(CGPoint::new(0.0, 0.0), CGSize::new(1.0, 1.0));
unsafe {
let view = UIStackView::new(mtm);
view.addArrangedSubview(&WgpuTriangleView::new(mtm, frame));
view.addArrangedSubview(&WgpuTriangleView::new(mtm, frame));
// view.setOrientation(NSUserInterfaceLayoutOrientation::Horizontal);
view.setDistribution(UIStackViewDistribution::FillEqually);
view_controller.setView(Some(&view));
}
} else {
view_controller.setView(Some(&WgpuTriangleView::new(mtm, frame)));
}

window.setRootViewController(Some(&view_controller));
Expand Down

0 comments on commit 8848e79

Please sign in to comment.