@rbxts/centurion-ui 2.0.0-rc.5 → 2.0.0-rc.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@rbxts/centurion-ui",
3
3
  "description": "Terminal UI for the Centurion command framework",
4
- "version": "2.0.0-rc.5",
4
+ "version": "2.0.0-rc.6",
5
5
  "author": "paradoxuum",
6
6
  "license": "MIT",
7
7
  "main": "src/ts/init.luau",
@@ -23,8 +23,12 @@ local spring = vide.spring
23
23
 
24
24
  type History = {
25
25
  read max_lines: number | () -> number,
26
+ -- the most room the history can take up, in pixels, so it fits on screen
27
+ read max_height: (() -> number)?,
26
28
  read on_select: (command: string) -> (),
27
29
  read order: number?,
30
+
31
+ [number]: unknown,
28
32
  }
29
33
 
30
34
  local line_height = 23
@@ -197,7 +201,11 @@ local function history(props: History)
197
201
  if height == 0 then
198
202
  return UDim2.new()
199
203
  end
200
- return UDim2.fromOffset(0, math.min(height, max_height()) + px.convert(top_padding))
204
+ height = math.min(height, max_height()) + px.convert(top_padding)
205
+ if props.max_height then
206
+ height = math.min(height, math.max(props.max_height(), 0))
207
+ end
208
+ return UDim2.fromOffset(0, height)
201
209
  end, 0.1),
202
210
 
203
211
  ScrollBarThickness = px(3),
@@ -257,6 +265,8 @@ local function history(props: History)
257
265
  indexes(state.logs, function(value: () -> types.Log, i: number)
258
266
  return entry(value, i, props.on_select)
259
267
  end),
268
+
269
+ unpack(props),
260
270
  }
261
271
  end
262
272
 
package/src/app/init.luau CHANGED
@@ -1,4 +1,5 @@
1
1
  local ContextActionService = game:GetService("ContextActionService")
2
+ local GuiService = game:GetService("GuiService")
2
3
  local Players = game:GetService("Players")
3
4
  local RunService = game:GetService("RunService")
4
5
  local UserInputService = game:GetService("UserInputService")
@@ -79,6 +80,22 @@ local function app()
79
80
  keyboard_visible(UserInputService.OnScreenKeyboardVisible)
80
81
  end))
81
82
 
83
+ -- top edge of the on-screen keyboard, in the same space as the console, or
84
+ -- nil when there isn't one taking up the screen
85
+ local function read_keyboard_top(): number?
86
+ if UserInputService.OnScreenKeyboardSize.Y <= 0 then
87
+ return nil
88
+ end
89
+ -- the keyboard is placed on the whole screen, while the console is placed below the topbar
90
+ return UserInputService.OnScreenKeyboardPosition.Y - GuiService:GetGuiInset().Y
91
+ end
92
+ local keyboard_top = source(read_keyboard_top())
93
+ local function update_keyboard_top()
94
+ keyboard_top(read_keyboard_top())
95
+ end
96
+ cleanup(UserInputService:GetPropertyChangedSignal("OnScreenKeyboardPosition"):Connect(update_keyboard_top))
97
+ cleanup(UserInputService:GetPropertyChangedSignal("OnScreenKeyboardSize"):Connect(update_keyboard_top))
98
+
82
99
  local next_cursor_pos = false
83
100
  local refocus_deadline = 0
84
101
  local cmd_history = {}
@@ -457,6 +474,76 @@ local function app()
457
474
  return if has_popover() then 1 else 0
458
475
  end, 0.12)
459
476
 
477
+ -- on short screens the history gives up room first, then the suggestions,
478
+ -- so that neither runs off the bottom of the screen
479
+ local bounds_bottom = source(math.huge)
480
+ local console_top = source(0)
481
+ -- height of the console without its history
482
+ local console_fixed = source(0)
483
+ local popover_height = source(0)
484
+ local frames: { [string]: GuiObject } = {}
485
+
486
+ local function measure()
487
+ local console, history_frame, popover = frames.console, frames.history, frames.popover
488
+ if console and history_frame then
489
+ console_top(console.AbsolutePosition.Y)
490
+ -- read together so both sizes are from the same layout pass
491
+ console_fixed(console.AbsoluteSize.Y - history_frame.AbsoluteSize.Y)
492
+ end
493
+ if popover then
494
+ popover_height(popover.AbsoluteSize.Y)
495
+ end
496
+ end
497
+
498
+ local function measured(name: string)
499
+ return action(function(instance: GuiObject)
500
+ frames[name] = instance
501
+ cleanup(instance:GetPropertyChangedSignal("AbsoluteSize"):Connect(measure))
502
+ cleanup(instance:GetPropertyChangedSignal("AbsolutePosition"):Connect(measure))
503
+ end)
504
+ end
505
+
506
+ -- room for the history and suggestions, between the input and the bottom of the screen
507
+ local function free_space()
508
+ local bottom = bounds_bottom()
509
+ -- shrink out of the way of the on-screen keyboard while it's open. consoles
510
+ -- are left alone, since their keyboard covers the whole screen anyway
511
+ local top_of_keyboard = keyboard_top()
512
+ if touch() and keyboard_visible() and top_of_keyboard then
513
+ bottom = math.min(bottom, top_of_keyboard)
514
+ end
515
+ return bottom - px.convert(8) - console_top() - console_fixed()
516
+ end
517
+
518
+ -- the bottom of whatever the console is drawn in, usually the ScreenGui
519
+ local track_bounds = action(function(instance: GuiObject)
520
+ local connection: RBXScriptConnection? = nil
521
+
522
+ local function update_parent()
523
+ if connection then
524
+ connection:Disconnect()
525
+ connection = nil
526
+ end
527
+
528
+ local parent = instance.Parent
529
+ if parent and parent:IsA("GuiBase2d") then
530
+ local function update()
531
+ bounds_bottom(parent.AbsolutePosition.Y + parent.AbsoluteSize.Y)
532
+ end
533
+ connection = parent:GetPropertyChangedSignal("AbsoluteSize"):Connect(update)
534
+ update()
535
+ end
536
+ end
537
+
538
+ cleanup(instance:GetPropertyChangedSignal("Parent"):Connect(update_parent))
539
+ cleanup(function()
540
+ if connection then
541
+ connection:Disconnect()
542
+ end
543
+ end)
544
+ update_parent()
545
+ end)
546
+
460
547
  -- created inside the console so that it fades with it
461
548
  local function input_field()
462
549
  return background {
@@ -631,6 +718,7 @@ local function app()
631
718
 
632
719
  padding(px(8)),
633
720
  flex():row():gap(1),
721
+ track_bounds,
634
722
 
635
723
  {
636
724
  Visible = function()
@@ -656,6 +744,7 @@ local function app()
656
744
  },
657
745
  flex():row():fill("horizontal"),
658
746
  catch_hover(),
747
+ measured("console"),
659
748
 
660
749
  header {
661
750
  order = 1,
@@ -673,6 +762,10 @@ local function app()
673
762
  history {
674
763
  order = 3,
675
764
  max_lines = max_history_lines,
765
+ -- the suggestions take priority, since they're what's being typed into
766
+ max_height = function()
767
+ return free_space() - (if has_popover() then popover_height() else 0)
768
+ end,
676
769
  on_select = function(command)
677
770
  cmd_history_index = 0
678
771
  raw_text(command)
@@ -680,6 +773,8 @@ local function app()
680
773
  -- after the textbox has recaptured focus, move the cursor to the end
681
774
  task.defer(cursor_pos, #command + 1)
682
775
  end,
776
+
777
+ measured("history"),
683
778
  },
684
779
 
685
780
  container {
@@ -713,10 +808,14 @@ local function app()
713
808
  return pop() > 0.01
714
809
  end,
715
810
  },
811
+ measured("popover"),
716
812
 
717
813
  suggestions {
718
814
  analysis = display_analysis,
719
815
  selected_suggestion = selected_suggestion,
816
+ max_height = function()
817
+ return free_space() - px.convert(4)
818
+ end,
720
819
  on_select = function(suggestion: types.AnalysisSuggestion)
721
820
  raw_text(autofill(raw_text(), suggestion, true))
722
821
  if not gamepad() then
@@ -33,6 +33,8 @@ export type Suggestions = {
33
33
  read analysis: () -> types.AnalysisResult,
34
34
  read selected_suggestion: () -> number,
35
35
  read on_select: (suggestion: types.AnalysisSuggestion) -> (),
36
+ -- the most room the popover can take up, in pixels, so it fits on screen
37
+ read max_height: (() -> number)?,
36
38
  read order: number?,
37
39
 
38
40
  [number]: unknown,
@@ -252,6 +254,19 @@ local function suggestions(props: Suggestions)
252
254
  -- set by the list, which is also at the bottom when it's too short to scroll
253
255
  local scrolled_to_bottom = source(true)
254
256
 
257
+ -- height of everything in the popover besides the list, which is what the
258
+ -- list has to leave room for when it's limited by max_height
259
+ local root: GuiObject? = nil
260
+ local list: GuiObject? = nil
261
+ local chrome_height = source(0)
262
+
263
+ local function measure()
264
+ if root and list then
265
+ -- read together so both sizes are from the same layout pass
266
+ chrome_height(root.AbsoluteSize.Y - list.AbsoluteSize.Y)
267
+ end
268
+ end
269
+
255
270
  return background {
256
271
  auto = "XY",
257
272
  order = props.order,
@@ -272,6 +287,10 @@ local function suggestions(props: Suggestions)
272
287
  end,
273
288
  },
274
289
  flex():row():fill("horizontal"),
290
+ action(function(instance: Frame)
291
+ root = instance
292
+ cleanup(instance:GetPropertyChangedSignal("AbsoluteSize"):Connect(measure))
293
+ end),
275
294
 
276
295
  -- why the input can't be run as it is
277
296
  show(failed, function()
@@ -406,7 +425,15 @@ local function suggestions(props: Suggestions)
406
425
  end
407
426
 
408
427
  local function visible_height()
409
- return math.min(content_height(), row_height() * state.visible_suggestions())
428
+ local height = math.min(content_height(), row_height() * state.visible_suggestions())
429
+ local row = row_height()
430
+ if props.max_height == nil or row == 0 then
431
+ return height
432
+ end
433
+
434
+ -- only whole rows, around the list's padding, but always at least one
435
+ local room = props.max_height() - chrome_height() - px.convert(6)
436
+ return math.min(height, math.max(math.floor(room / row), 1) * row)
410
437
  end
411
438
 
412
439
  effect(function()
@@ -480,6 +507,13 @@ local function suggestions(props: Suggestions)
480
507
  changed("CanvasPosition", function(position: Vector2)
481
508
  canvas_y(position.Y)
482
509
  end),
510
+ action(function(instance: ScrollingFrame)
511
+ list = instance
512
+ cleanup(instance:GetPropertyChangedSignal("AbsoluteSize"):Connect(measure))
513
+ cleanup(function()
514
+ list = nil
515
+ end)
516
+ end),
483
517
 
484
518
  padding(px(3)),
485
519