1package core
2
3import (
4 "context"
5 "slices"
6
7 "github.com/bnema/neferwl/internal/ports"
8)
9
10// sceneFor builds the scene of screen i and sends the configures it sizes.
11// drawable reports that it can show the focus pulse on the focused window.
12func (c *Core) sceneFor(ctx context.Context, i int, sc *screen, only *screen, capture *capView, pulse float64) (scene ports.Scene, drawable bool, err error) {
13 o := sc.mon.Output()
14 // view.frame is the viewport of the workspace on screen: the whole
15 // output unless it has a size override (never in the overview).
16 view := sc.mon.viewport()
17 var clip Rect
18 if view.frame != (Rect{W: o.W, H: o.H}) {
19 clip = view.frame
20 }
21 // layout is what is drawn; settled (same indexes) is where the
22 // windows are going, and alone sizes the configures.
23 layout, settled := sc.shown, sc.settledLayout
24 scene = ports.Scene{Security: c.security, Output: sc.name(), OutputWidth: o.W, OutputHeight: o.H, WorkspaceClip: clip, Scale: sc.scale, Transform: sc.transform, Off: sc.off, Background: c.cfg.Background.Color, Border: ports.Border{Width: c.cfg.Border.Width, Active: c.cfg.Border.Active, Inactive: c.cfg.Border.Inactive}, Windows: make([]ports.SceneWindow, 0, len(layout)+len(c.popupOrder)), Layers: shownLayers(sc)}
25 if !sc.mon.previewing() {
26 // Tiles stop at the panels; the overview lays out previews over
27 // the whole output.
28 u := view.usable
29 scene.TileInset = ports.Insets{Top: u.Y, Left: u.X, Right: o.W - u.X - u.W, Bottom: o.H - u.Y - u.H}
30 }
31 var real map[WindowID]Placement
32 if sc.mon.previewing() {
33 // Local to this screen's build: reused, cleared each time.
34 if c.overviewReal == nil {
35 c.overviewReal = make(map[WindowID]Placement)
36 }
37 clear(c.overviewReal)
38 real = c.overviewReal
39 for w := range sc.mon.all() {
40 c.realBuf = w.layoutInto(c.realBuf)
41 for _, p := range c.realBuf {
42 real[p.ID] = p
43 }
44 }
45 }
46 scene.Dim = floatDim(layout, view, c.cfg.Floating.Dim)
47 if sc.mon.previewing() {
48 // Darken the wallpaper around the previews.
49 scene.Dim = c.cfg.Floating.Dim
50 scene.DimBehind = scene.Dim > 0
51 }
52 if d := c.drag; d != nil && d.target.screen == sc && d.target.kind != dropNone {
53 scene.DropHints = slices.Clone(d.target.hints)
54 }
55 if sc.mon.ov.open {
56 // Frame the selection and separate numbered and named row groups.
57 _, scene.Separators = sc.mon.overviewRows()
58 scene.Separators = append(scene.Separators, overviewOutline(layout, max(c.cfg.Border.Width, 2))...)
59 } else if sc.mon.switcherShown() {
60 // The switcher's cards: only the selection is framed.
61 scene.Separators = overviewOutline(layout, max(c.cfg.Border.Width, 2))
62 } else {
63 // Only the focused output lights the focused window's lines.
64 scene.Separators = separators(layout, c.cfg.Border.Width, sc.mon.Current().gap(), view, i == c.focusScreen)
65 }
66 // A window alone on screen needs no pulse to show it has the focus.
67 alone := i == c.focusScreen && c.pulse.target != 0 && visibleCount(layout, view) == 1
68 for k, p := range layout {
69 ps := settled[k]
70 // Only the focused output has an activated window.
71 focused := p.Focused && i == c.focusScreen
72 sw := ports.SceneWindow{ID: p.ID, Rect: p.Rect, Focused: focused, Fullscreen: p.Fullscreen, Hidden: p.Hidden, Floating: p.Floating, Below: p.Below, Inset: p.Inset, Preview: p.Preview, Fade: p.Fade}
73 if p.Zoom > 0 && (p.Zoom < 1 || p.Preview > 0) {
74 // A scale motion: the content follows the drawn size. A
75 // card in flight drawn at its size still needs Zoom 1:
76 // without it the renderer would shrink it by Preview.
77 sw.Zoom = p.Zoom
78 }
79 // A peek's veil is the configured one plus its animated offset.
80 sw.Dim = max(0, min(p.Dim+c.peekDim(p), 1))
81 if p.Leaving {
82 // A window fading out after it closed or hid: drawn, but
83 // hidden to everything else, its configures included.
84 sw.Hidden = false
85 scene.Windows = append(scene.Windows, sw)
86 // A window the workspace still holds (a hidden stash
87 // window) keeps its last configure: prune would forget it,
88 // and its next one would start from nothing. A closed one
89 // was forgotten at its unmap and stays so.
90 if w, _ := sc.mon.find(p.ID); w != nil {
91 c.configures.keep(p.ID)
92 }
93 continue
94 }
95 if focused && p.ID == c.pulse.target && !alone && !p.Fullscreen && !p.Hidden && p.Preview == 0 && !sc.mon.previewing() && hasRoom(view, p) {
96 drawable = true
97 if p.ID == c.pulse.id {
98 sw.FocusEffect = pulse
99 }
100 }
101 scene.Windows = append(scene.Windows, sw)
102 t := configureTarget{output: sc.name(), view: view, focused: focused, captured: capture != nil && capture.window == p.ID}
103 if !p.Hidden && p.Preview == 0 {
104 // Only a sized configure needs the client size.
105 t.client, t.imposed = c.clientRect(ps), sc.mon.Current().imposedFloat(p.ID)
106 } else if t.captured && p.Hidden {
107 // A captured hidden window is sized like its capture.
108 t.client = capture.windowSz
109 } else if p.Preview > 0 && !p.Hidden {
110 if rp, ok := real[p.ID]; ok && !rp.Hidden {
111 t.realTiled = !rp.Floating
112 t.client = c.clientRect(rp)
113 }
114 }
115 cp, t := c.captureConfigure(sc, ps, t)
116 if v, send := c.configures.nextWithCapture(ps, t, cp); send {
117 if err := c.command(ctx, v); err != nil {
118 return ports.Scene{}, false, err
119 }
120 c.configures.mark(v)
121 }
122 }
123 scene.Windows = append(scene.Windows, c.scenePopups(sc)...)
124 scene.CaptureIndicators = c.captureIndicators(sc)
125 scene.Capture = c.captureSceneFor(sc, capture)
126 // The springs of this frame are advanced: a scene shown while one still
127 // runs wakes core when it flips; the one a spring settles on does not.
128 scene.Animating = c.sliding(sc.name())
129 // A scene carrying a capture image always gets a fresh Seq, and so
130 // does one of an animating screen (the one being stepped, or any
131 // when none is): the outputs drop a scene they already show, and a
132 // spring that starts at the previous scene, or rounds to it in its
133 // tail, would get no flip and wait for the fallback timer. So does the
134 // scene that flips Animating off: SameAs ignores the flag, and keeping
135 // the Seq of the animating scene would leave outputs showing it, and
136 // waking core on every later flip. Any other keeps its output's Seq
137 // while it draws the same, so an idle output is not recomposed.
138 withCapture := scene.Capture != nil && (sc == capture.hiddenScr || sc == capture.windowScr)
139 animating := scene.Animating && (only == nil || only == sc)
140 if !withCapture && !animating && sc.last.Animating == scene.Animating && sc.last.Seq != 0 && scene.SameAs(sc.last) {
141 scene.Seq = sc.last.Seq
142 } else {
143 c.seq++
144 scene.Seq = c.seq
145 }
146 if withCapture {
147 switch sc {
148 case capture.hiddenScr:
149 scene.CaptureScene = c.captureScene(scene.Seq)
150 case capture.windowScr:
151 scene.CaptureScene = c.captureWindowScene(capture, scene.Seq)
152 }
153 }
154 return scene, drawable, nil
155}