Skip to content

Commit

Permalink
WIP: Replace reflow+cascade with refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Jun 5, 2024
1 parent bb44dca commit e834ceb
Show file tree
Hide file tree
Showing 13 changed files with 378 additions and 433 deletions.
43 changes: 24 additions & 19 deletions demo/src/main/scala/demo/ComponentsWindow2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ object ComponentsWindow2:
// )
// .withBoundsType(BoundsType.Fixed(Bounds(0, 0, 20, 2)))
)
.withBoundsType(BoundsType.Fixed(Bounds(0, 0, 20, 2)))
// .add(
// Label("label 1", Label.Theme(charSheet)),
// Label("label 2", Label.Theme(charSheet)),
// Label("label 3", Label.Theme(charSheet))
// )
// .withBoundsType(BoundsType.Fixed(Bounds(0, 0, 20, 2)))
// .add(
// ComponentGroup()
// .withLayout(ComponentLayout.Horizontal(Padding(0, 1, 0, 0)))
Expand All @@ -49,26 +54,26 @@ object ComponentsWindow2:
// }
// .withLayout(ComponentLayout.Vertical())
// )
.add(
ComponentList(Bounds(0, 0, 20, 3)) { (count: Int) =>
Batch(Label[Int]("How many windows: ", Label.Theme(charSheet))) ++
Batch.fill(count)(Label("x", Label.Theme(charSheet)))
}
.add((count: Int) =>
Batch.fill(count)(
Button[Int]("Button", Button.Theme(charSheet)).onClick(Log("count: " + count))
)
:+ Button[Int]("test", Button.Theme(charSheet)).onClick(Log("test"))
)
.add((i: Int) => TextArea[Int]("abc.\nde,f\n0123456! " + i, TextArea.Theme(charSheet)))
.withLayout(ComponentLayout.Vertical(Padding.zero))
)
// .add(
// Input(20, Input.Theme(charSheet))
// )
// .add(
// TextArea("abc.\nde,f\n0123456!", TextArea.Theme(charSheet))
// ComponentList(Bounds(0, 0, 20, 8)) { (count: Int) =>
// Batch(Label[Int]("How many windows: ", Label.Theme(charSheet))) ++
// Batch.fill(count)(Label("x", Label.Theme(charSheet)))
// }
// .add((count: Int) =>
// Batch.fill(count)(
// Button[Int]("Button", Button.Theme(charSheet)).onClick(Log("count: " + count))
// )
// :+ Button[Int]("test", Button.Theme(charSheet)).onClick(Log("test"))
// )
// .add((i: Int) => TextArea[Int]("abc.\nde,f\n0123456! " + i, TextArea.Theme(charSheet)))
// .withLayout(ComponentLayout.Vertical(Padding.zero))
// )
.add(
Input(20, Input.Theme(charSheet))
)
// .add(
// TextArea("abc.\nde,f\n0123456!", TextArea.Theme(charSheet))
// )
)
.withTitle("More component examples")
.moveTo(2, 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ trait Component[A, ReferenceData]:
): Outcome[ComponentFragment]

/** Used internally to instruct the component that the layout has changed in some way, and that it
* should reflow it's contents - whatever that means in the context of this component type.
* should reflow/refresh it's contents - whatever that means in the context of this component
* type.
*/
def reflow(reference: ReferenceData, model: A): A

/** Informs the Component that something about its parent has changed in case it needs to take
* action. Currently the only cascaded change is the bounds.
*/
def cascade(model: A, parentBounds: Bounds): A
def refresh(reference: ReferenceData, model: A, parentBounds: Bounds): A
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ trait StatelessComponent[A, ReferenceData] extends Component[A, ReferenceData]:
model: A
): Outcome[ComponentFragment]

def reflow(reference: ReferenceData, model: A): A = model
def cascade(model: A, parentBounds: Bounds): A = model
def refresh(reference: ReferenceData, model: A, parentBounds: Bounds): A = model
def updateModel(context: UiContext[ReferenceData], model: A): GlobalEvent => Outcome[A] =
case e => Outcome(model)
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,7 @@ object Input:
context.running
)

def reflow(reference: ReferenceData, model: Input): Input =
model

def cascade(model: Input, parentBounds: Bounds): Input =
def refresh(reference: ReferenceData, model: Input, parentBounds: Bounds): Input =
model

final case class Theme(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,4 @@ final case class ComponentEntry[A, ReferenceData](
offset: Coords,
model: A,
component: Component[A, ReferenceData]
):

def cascade(parentBounds: Bounds): ComponentEntry[A, ReferenceData] =
this.copy(model = component.cascade(model, parentBounds))
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,33 @@ import roguelikestarterkit.ui.datatypes.Dimensions

/** Describes how a ComponentGroup responds to changes in its parents bounds.
*/
enum BoundsType:

/** The component group ignores parent bounds changes
*/
case Fixed(bounds: Bounds)

/** The component group uses its parents bounds
*/
case Inherit

/** The component group positions and resizes itself within / based on the parents bounds
* according to percentages expressed as a value from 0.0 to 1.0, e.g. 50% is 0.5
*/
case Relative(x: Double, y: Double, width: Double, height: Double)

/** The component group positions itself within the parents bounds according to percentages
* expressed as a value from 0.0 to 1.0, e.g. 50% is 0.5
*/
case RelativePosition(x: Double, y: Double)

/** The component group resizes itself based on the parents bounds according to percentages
* expressed as a value from 0.0 to 1.0, e.g. 50% is 0.5
*/
case RelativeSize(width: Double, height: Double)

/** The component group positions and resizes itself within / based on the parents bounds, offset
* by the amounts given.
*/
case Offset(coords: Coords, dimensions: Dimensions)

/** The component group positions itself within the parents bounds, offset by the amount given.
*/
case OffsetPosition(coords: Coords)

/** The component group resizes itself based on the parents bounds, offset by the amount given.
*/
case OffsetSize(dimensions: Dimensions)

/** The component group bases its size on some aspect of its contents or the available space
*/
case Dynamic(width: FitMode, height: FitMode)
final case class BoundsType(width: FitMode, height: FitMode)

object BoundsType:

val default: BoundsType =
BoundsType.Dynamic(FitMode.Available, FitMode.Content)
BoundsType(FitMode.Available, FitMode.Content)

def fixed(dimensions: Dimensions): BoundsType =
BoundsType(
FitMode.Fixed(dimensions.width),
FitMode.Fixed(dimensions.height)
)

def inherit: BoundsType =
BoundsType(FitMode.Available, FitMode.Available)

def fit: BoundsType =
BoundsType(FitMode.Content, FitMode.Content)

def halfHorizontal: BoundsType =
BoundsType(
FitMode.Relative(0.5),
FitMode.Available
)

def halfVertical: BoundsType =
BoundsType(
FitMode.Available,
FitMode.Relative(0.5)
)
Loading

0 comments on commit e834ceb

Please sign in to comment.