289 lines
13 KiB
Swift
289 lines
13 KiB
Swift
// Generated by PorticoGen. DO NOT EDIT. See Sources/PorticoGen to make changes.
|
||
|
||
import Adw
|
||
import Gtk
|
||
import Gio
|
||
import Gdk
|
||
|
||
// PorticoGen: generateStruct | source: Gtk.DrawingArea
|
||
/// Allows drawing with cairo.
|
||
///
|
||
/// <picture>
|
||
/// <source srcset="drawingarea-dark.png" media="(prefers-color-scheme: dark)">
|
||
/// <img alt="An example GtkDrawingArea" src="drawingarea.png">
|
||
/// </picture>
|
||
///
|
||
/// It’s essentially a blank widget; you can draw on it. After
|
||
/// creating a drawing area, the application may want to connect to:
|
||
///
|
||
/// - The [signal`Gtk`.Widget::realize] signal to take any necessary actions
|
||
/// when the widget is instantiated on a particular display.
|
||
/// (Create GDK resources in response to this signal.)
|
||
///
|
||
/// - The [signal`Gtk`.DrawingArea::resize] signal to take any necessary
|
||
/// actions when the widget changes size.
|
||
///
|
||
/// - Call [method`Gtk`.DrawingArea.set_draw_func] to handle redrawing the
|
||
/// contents of the widget.
|
||
///
|
||
/// The following code portion demonstrates using a drawing
|
||
/// area to display a circle in the normal widget foreground
|
||
/// color.
|
||
///
|
||
/// ## Simple GtkDrawingArea usage
|
||
///
|
||
/// ```c
|
||
/// static void
|
||
/// draw_function (GtkDrawingArea *area,
|
||
/// cairo_t *cr,
|
||
/// int width,
|
||
/// int height,
|
||
/// gpointer data)
|
||
/// {
|
||
/// GdkRGBA color;
|
||
///
|
||
/// cairo_arc (cr,
|
||
/// width / 2.0, height / 2.0,
|
||
/// MIN (width, height) / 2.0,
|
||
/// 0, 2 * G_PI);
|
||
///
|
||
/// gtk_widget_get_color (GTK_WIDGET (area),
|
||
/// &color);
|
||
/// gdk_cairo_set_source_rgba (cr, &color);
|
||
///
|
||
/// cairo_fill (cr);
|
||
/// }
|
||
///
|
||
/// int
|
||
/// main (int argc, char **argv)
|
||
/// {
|
||
/// gtk_init ();
|
||
///
|
||
/// GtkWidget *area = gtk_drawing_area_new ();
|
||
/// gtk_drawing_area_set_content_width (GTK_DRAWING_AREA (area), 100);
|
||
/// gtk_drawing_area_set_content_height (GTK_DRAWING_AREA (area), 100);
|
||
/// gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (area),
|
||
/// draw_function,
|
||
/// NULL, NULL);
|
||
/// return 0;
|
||
/// }
|
||
/// ```
|
||
///
|
||
/// The draw function is normally called when a drawing area first comes
|
||
/// onscreen, or when it’s covered by another window and then uncovered.
|
||
/// You can also force a redraw by adding to the “damage region” of the
|
||
/// drawing area’s window using [method`Gtk`.Widget.queue_draw].
|
||
/// This will cause the drawing area to call the draw function again.
|
||
///
|
||
/// The available routines for drawing are documented in the
|
||
/// [Cairo documentation](https://www.cairographics.org/manual/); GDK
|
||
/// offers additional API to integrate with Cairo, like [func`Gdk`.cairo_set_source_rgba]
|
||
/// or [func`Gdk`.cairo_set_source_pixbuf].
|
||
///
|
||
/// To receive mouse events on a drawing area, you will need to use
|
||
/// event controllers. To receive keyboard events, you will need to set
|
||
/// the “can-focus” property on the drawing area, and you should probably
|
||
/// draw some user-visible indication that the drawing area is focused.
|
||
///
|
||
/// If you need more complex control over your widget, you should consider
|
||
/// creating your own `GtkWidget` subclass.
|
||
///
|
||
/// A Portico view that mounts a `Gtk.DrawingArea`.
|
||
@MainActor public struct DrawingArea: View {
|
||
private let make: (MountContext) -> Gtk.DrawingArea
|
||
private var configure: [(Gtk.DrawingArea, MountContext) -> Void] = []
|
||
|
||
public var body: Never { fatalError() }
|
||
|
||
// PorticoGen: generateInits(static) | source: Gtk.DrawingArea.init()
|
||
/// Creates a new drawing area.
|
||
///
|
||
/// Applied once at mount; use the `Binding` or closure overload for values that change.
|
||
/// Optional value parameters are applied only when non-`nil`; a `nil` argument leaves the widget's own default in place and cannot clear a nullable property - use the matching modifier for that.
|
||
///
|
||
/// - Parameter contentHeight: The content height.
|
||
/// - Parameter contentWidth: The content width.
|
||
/// - Parameter onResize: Invoked when the widget emits the `resize` signal. The closure receives the signal's arguments in order.
|
||
public init(contentHeight: Int32? = nil, contentWidth: Int32? = nil, onResize: ((Int32, Int32) -> Void)? = nil) {
|
||
make = { _ in Gtk.DrawingArea() }
|
||
configure.append { w, ctx in
|
||
if let contentHeight { w.setContentHeight(height: contentHeight) }
|
||
if let contentWidth { w.setContentWidth(width: contentWidth) }
|
||
if let onResize { ctx.registry.add(w.connectResize { _, a0, a1 in onResize(a0, a1) }) }
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
extension DrawingArea: WidgetView {
|
||
public typealias Target = Gtk.DrawingArea
|
||
|
||
@_spi(Portico) public func appending(
|
||
_ step: @escaping (Gtk.DrawingArea, MountContext) -> Void
|
||
) -> Self {
|
||
var c = self
|
||
c.configure.append(step)
|
||
return c
|
||
}
|
||
}
|
||
|
||
@_spi(Portico) extension DrawingArea: Mountable {
|
||
@_spi(Portico) public func mount(_ ctx: MountContext) -> Gtk.Widget {
|
||
let w = make(ctx)
|
||
for step in configure { step(w, ctx) }
|
||
return w
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension | source: Gtk.DrawingArea
|
||
/// Modifiers for `Gtk.DrawingArea`, available on every Portico view whose
|
||
/// backing widget is `Gtk.DrawingArea` or one of its subclasses.
|
||
extension WidgetView where Target: Gtk.DrawingArea {
|
||
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(static) | source: Gtk.DrawingArea.setContentHeight(height:)
|
||
/// Sets the desired height of the contents of the drawing area.
|
||
///
|
||
/// Note that because widgets may be allocated larger sizes than they
|
||
/// requested, it is possible that the actual height passed to your draw
|
||
/// function is larger than the height set here. You can use
|
||
/// [method`Gtk`.Widget.set_valign] to avoid that.
|
||
///
|
||
/// If the height is set to 0 (the default), the drawing area may disappear.
|
||
///
|
||
/// Applied once at mount; use the `Binding` or closure overload for a value that changes.
|
||
///
|
||
/// - Parameter contentHeight: The content height.
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func contentHeight(_ contentHeight: Int32) -> Self {
|
||
appending { w, _ in
|
||
w.setContentHeight(height: contentHeight)
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension -> generatePropertyModifiers -> bindingModifier(twoWay) | source: Gtk.DrawingArea.setContentHeight(height:), GObject.Object.connectNotify(detail:_:), Gtk.DrawingArea.getContentHeight()
|
||
/// Sets the desired height of the contents of the drawing area.
|
||
///
|
||
/// Note that because widgets may be allocated larger sizes than they
|
||
/// requested, it is possible that the actual height passed to your draw
|
||
/// function is larger than the height set here. You can use
|
||
/// [method`Gtk`.Widget.set_valign] to avoid that.
|
||
///
|
||
/// If the height is set to 0 (the default), the drawing area may disappear.
|
||
///
|
||
/// Applied at mount and re-applied on every change the binding publishes.
|
||
/// When `Int32` conforms to `Equatable` this binds in both directions: the widget's `notify` signal writes its current value back into the binding, so changes made in the UI propagate to the bound state. Each direction compares before writing, which terminates the echo after one hop. A value type that is not `Equatable` binds one way only, because the echo cannot be broken.
|
||
///
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func contentHeight(_ contentHeight: Portico.Binding<Int32>) -> Self {
|
||
appending { w, ctx in
|
||
Portico.bindProperty(
|
||
w, contentHeight, registry: ctx.registry, notifyDetail: "content-height",
|
||
read: { [w] in w.getContentHeight() },
|
||
write: { [w] v in w.setContentHeight(height: v) }
|
||
)
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(closure) | source: Gtk.DrawingArea.setContentHeight(height:)
|
||
/// Sets the desired height of the contents of the drawing area.
|
||
///
|
||
/// Note that because widgets may be allocated larger sizes than they
|
||
/// requested, it is possible that the actual height passed to your draw
|
||
/// function is larger than the height set here. You can use
|
||
/// [method`Gtk`.Widget.set_valign] to avoid that.
|
||
///
|
||
/// If the height is set to 0 (the default), the drawing area may disappear.
|
||
///
|
||
/// The closure runs inside a `DependencyTracker`, so any `@State` it reads re-runs it and pushes the new value through `Gtk.DrawingArea.setContentHeight(height:)`.
|
||
///
|
||
/// - Parameter contentHeight: The content height.
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func contentHeight(_ contentHeight: @escaping () -> Int32) -> Self {
|
||
appending { w, ctx in
|
||
let tracker = DependencyTracker { [w] in w.setContentHeight(height: contentHeight()) }
|
||
tracker.run()
|
||
ctx.registry.add(tracker)
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(static) | source: Gtk.DrawingArea.setContentWidth(width:)
|
||
/// Sets the desired width of the contents of the drawing area.
|
||
///
|
||
/// Note that because widgets may be allocated larger sizes than they
|
||
/// requested, it is possible that the actual width passed to your draw
|
||
/// function is larger than the width set here. You can use
|
||
/// [method`Gtk`.Widget.set_halign] to avoid that.
|
||
///
|
||
/// If the width is set to 0 (the default), the drawing area may disappear.
|
||
///
|
||
/// Applied once at mount; use the `Binding` or closure overload for a value that changes.
|
||
///
|
||
/// - Parameter contentWidth: The content width.
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func contentWidth(_ contentWidth: Int32) -> Self {
|
||
appending { w, _ in
|
||
w.setContentWidth(width: contentWidth)
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension -> generatePropertyModifiers -> bindingModifier(twoWay) | source: Gtk.DrawingArea.setContentWidth(width:), GObject.Object.connectNotify(detail:_:), Gtk.DrawingArea.getContentWidth()
|
||
/// Sets the desired width of the contents of the drawing area.
|
||
///
|
||
/// Note that because widgets may be allocated larger sizes than they
|
||
/// requested, it is possible that the actual width passed to your draw
|
||
/// function is larger than the width set here. You can use
|
||
/// [method`Gtk`.Widget.set_halign] to avoid that.
|
||
///
|
||
/// If the width is set to 0 (the default), the drawing area may disappear.
|
||
///
|
||
/// Applied at mount and re-applied on every change the binding publishes.
|
||
/// When `Int32` conforms to `Equatable` this binds in both directions: the widget's `notify` signal writes its current value back into the binding, so changes made in the UI propagate to the bound state. Each direction compares before writing, which terminates the echo after one hop. A value type that is not `Equatable` binds one way only, because the echo cannot be broken.
|
||
///
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func contentWidth(_ contentWidth: Portico.Binding<Int32>) -> Self {
|
||
appending { w, ctx in
|
||
Portico.bindProperty(
|
||
w, contentWidth, registry: ctx.registry, notifyDetail: "content-width",
|
||
read: { [w] in w.getContentWidth() },
|
||
write: { [w] v in w.setContentWidth(width: v) }
|
||
)
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(closure) | source: Gtk.DrawingArea.setContentWidth(width:)
|
||
/// Sets the desired width of the contents of the drawing area.
|
||
///
|
||
/// Note that because widgets may be allocated larger sizes than they
|
||
/// requested, it is possible that the actual width passed to your draw
|
||
/// function is larger than the width set here. You can use
|
||
/// [method`Gtk`.Widget.set_halign] to avoid that.
|
||
///
|
||
/// If the width is set to 0 (the default), the drawing area may disappear.
|
||
///
|
||
/// The closure runs inside a `DependencyTracker`, so any `@State` it reads re-runs it and pushes the new value through `Gtk.DrawingArea.setContentWidth(width:)`.
|
||
///
|
||
/// - Parameter contentWidth: The content width.
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func contentWidth(_ contentWidth: @escaping () -> Int32) -> Self {
|
||
appending { w, ctx in
|
||
let tracker = DependencyTracker { [w] in w.setContentWidth(width: contentWidth()) }
|
||
tracker.run()
|
||
ctx.registry.add(tracker)
|
||
}
|
||
}
|
||
|
||
// PorticoGen: generateModifierExtension -> generateSignalModifier | source: Gtk.DrawingArea.connectResize(_:)
|
||
/// Emitted once when the widget is realized, and then each time the widget
|
||
/// is changed while realized.
|
||
///
|
||
/// This is useful in order to keep state up to date with the widget size,
|
||
/// like for instance a backing surface.
|
||
///
|
||
/// - Parameter handler: Invoked when the widget emits the `resize` signal. The closure receives the signal's arguments in order.
|
||
/// - Returns: A copy of this view with the modifier applied.
|
||
public func onResize(_ handler: @escaping (Int32, Int32) -> Void) -> Self {
|
||
appending { w, ctx in
|
||
ctx.registry.add(w.connectResize { _, a0, a1 in handler(a0, a1) })
|
||
}
|
||
}
|
||
|
||
}
|