From ed962118698e6c070d84711a07b4f9396127748a Mon Sep 17 00:00:00 2001 From: Gavin Laking Date: Sat, 21 May 2016 20:18:16 +0100 Subject: [PATCH] Calculate y and x for alignment methods, also; Expose 'Vedeu.centre_x' and 'Vedeu.centre_y'. This fixed a bug in the Geometries::DSL exhibited in some examples where moving the interface caused it to 'jump' to the wrong location because either x or y is not set. --- docs/dsl/by_method/centre_x.md | 6 +++++ docs/dsl/by_method/centre_y.md | 6 +++++ lib/vedeu/geometries/dsl/dsl.rb | 40 ++++++++++++++++++++++++++++----- lib/vedeu/terminal/terminal.rb | 6 +++++ test/lib/vedeu_test.rb | 2 ++ 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 docs/dsl/by_method/centre_x.md create mode 100644 docs/dsl/by_method/centre_y.md diff --git a/docs/dsl/by_method/centre_x.md b/docs/dsl/by_method/centre_x.md new file mode 100644 index 000000000..294932799 --- /dev/null +++ b/docs/dsl/by_method/centre_x.md @@ -0,0 +1,6 @@ +### Vedeu.centre_x + +Provides the horizontal centre coordinate for the terminal. For +example, a terminal of width 40 characters: + + Vedeu.centre_x # => 20 diff --git a/docs/dsl/by_method/centre_y.md b/docs/dsl/by_method/centre_y.md new file mode 100644 index 000000000..21a6fcb58 --- /dev/null +++ b/docs/dsl/by_method/centre_y.md @@ -0,0 +1,6 @@ +### Vedeu.centre_y + +Provides the vertical centre coordinate for the terminal. For example, +a terminal of height 40 lines: + + Vedeu.centre_y # => 20 diff --git a/lib/vedeu/geometries/dsl/dsl.rb b/lib/vedeu/geometries/dsl/dsl.rb index f73e4d83e..95a4cffa2 100644 --- a/lib/vedeu/geometries/dsl/dsl.rb +++ b/lib/vedeu/geometries/dsl/dsl.rb @@ -105,23 +105,27 @@ def align(vertical: :none, horizontal: :none, width: nil, height: nil) # @param value [Symbol] One of :center, :centre, :left, :none, # :right. # @param width [Fixnum] The number of characters/columns. + # @param x [Fixnum] When given, sets the x coordinate. # @return [Vedeu::Geometries::Geometry] - def horizontal_alignment(value = :none, width = nil) + def horizontal_alignment(value = :none, width = nil, x = nil) alignment = Vedeu::Coercers::HorizontalAlignment.validate(value) model.width = width if width model.horizontal_alignment = alignment + model.x = x if x model end # @param value [Symbol] One of :bottom, :middle, :none, :top. # @param height [Fixnum] The number of lines/rows. + # @param y [Fixnum] When given, sets the y coordinate. # @return [Vedeu::Geometries::Geometry] - def vertical_alignment(value = :none, height = nil) + def vertical_alignment(value = :none, height = nil, y = nil) alignment = Vedeu::Coercers::VerticalAlignment.validate(value) model.height = height if height model.vertical_alignment = alignment + model.y = y if y model end @@ -129,14 +133,26 @@ def vertical_alignment(value = :none, height = nil) # @param height [Fixnum] The number of lines/rows. # @return [Vedeu::Geometries::Geometry] def align_bottom(height = nil) - vertical_alignment(:bottom, height) + y = if height + Vedeu.height - height + elsif model.height + Vedeu.height - model.height + end + + vertical_alignment(:bottom, height, y) end # {include:file:docs/dsl/by_method/geometry/align_centre.md} # @param width [Fixnum] The number of characters/columns. # @return [Vedeu::Geometries::Geometry] def align_centre(width = nil) - horizontal_alignment(:centre, width) + x = if width + Vedeu.centre_x - (width / 2) + elsif model.width + Vedeu.centre_x - (model.width / 2) + end + + horizontal_alignment(:centre, width, x) end alias align_center align_centre @@ -151,14 +167,26 @@ def align_left(width = nil) # @param height [Fixnum] The number of lines/rows. # @return [Vedeu::Geometries::Geometry] def align_middle(height = nil) - vertical_alignment(:middle, height) + y = if height + Vedeu.centre_y - (height / 2) + elsif model.height + Vedeu.centre_y - (model.height / 2) + end + + vertical_alignment(:middle, height, y) end # {include:file:docs/dsl/by_method/geometry/align_right.md} # @param width [Fixnum] The number of characters/columns. # @return [Vedeu::Geometries::Geometry] def align_right(width = nil) - horizontal_alignment(:right, width) + x = if width + Vedeu.width - width + elsif model.width + Vedeu.width - model.width + end + + horizontal_alignment(:right, width, x) end # {include:file:docs/dsl/by_method/geometry/align_top.md} diff --git a/lib/vedeu/terminal/terminal.rb b/lib/vedeu/terminal/terminal.rb index 7a65d5023..284305e8d 100644 --- a/lib/vedeu/terminal/terminal.rb +++ b/lib/vedeu/terminal/terminal.rb @@ -166,7 +166,13 @@ def console # @api public # @!method resize # @see Vedeu::Terminal#resize + # @!method centre_x + # @see Vedeu::Terminal#centre_x + # @!method centre_y + # @see Vedeu::Terminal#centre_y def_delegators Vedeu::Terminal, + :centre_x, + :centre_y, :resize end # Vedeu diff --git a/test/lib/vedeu_test.rb b/test/lib/vedeu_test.rb index 10ad32cbe..56ae61911 100644 --- a/test/lib/vedeu_test.rb +++ b/test/lib/vedeu_test.rb @@ -17,6 +17,8 @@ it { Vedeu.must_respond_to(:buffers) } it { Vedeu.must_respond_to(:buffer_update) } it { Vedeu.must_respond_to(:buffer_write) } + it { Vedeu.must_respond_to(:centre_x) } + it { Vedeu.must_respond_to(:centre_y) } it { Vedeu.must_respond_to(:clear) } it { Vedeu.must_respond_to(:clear_by_group) } it { Vedeu.must_respond_to(:clear_by_name) }