From 15f32b5a00145de23eb1aaaa36e28c13c2987b8a Mon Sep 17 00:00:00 2001 From: Noah Gibbs Date: Tue, 11 Jul 2023 13:03:29 +0100 Subject: [PATCH] Move constants into a shoes.rb to start off shoes.rb as separate --- .yardopts | 2 ++ lib/scarpe.rb | 16 ++-------- lib/scarpe/constants.rb | 24 --------------- lib/shoes.rb | 65 +++++++++++++++++++++++++++++++++++++++++ lib/shoes/constants.rb | 26 +++++++++++++++++ 5 files changed, 95 insertions(+), 38 deletions(-) delete mode 100644 lib/scarpe/constants.rb create mode 100644 lib/shoes.rb create mode 100644 lib/shoes/constants.rb diff --git a/.yardopts b/.yardopts index c5a262f73..0c617445e 100644 --- a/.yardopts +++ b/.yardopts @@ -3,6 +3,8 @@ --protected --no-private --template-path docs/yard/template +--tag incompatibility:"Incompatibilities with Shoes" +--exclude lib/scarpe/libui - lib/**/*.rb docs/yard/*.md diff --git a/lib/scarpe.rb b/lib/scarpe.rb index c33911c0b..3e52019b9 100644 --- a/lib/scarpe.rb +++ b/lib/scarpe.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require "shoes" + require_relative "scarpe/logger" # This will never be triggered -- we use the (...) feature below, which means this @@ -15,7 +17,6 @@ # Is there a Shoes::Error class? Should this be two different error classes? class Scarpe::Error < StandardError; end -require_relative "scarpe/constants" require_relative "scarpe/version" require_relative "scarpe/promises" require_relative "scarpe/display_service" @@ -26,16 +27,3 @@ class Scarpe::Error < StandardError; end d_s = ENV["SCARPE_DISPLAY_SERVICE"] || "wv_local" # This is require, not require_relative, to allow gems to supply a new display service require "scarpe/#{d_s}" - -include Constants - -module Shoes - class << self - def app(...) - app = Shoes::App.new(...) - app.init - app.run - app.destroy - end - end -end diff --git a/lib/scarpe/constants.rb b/lib/scarpe/constants.rb deleted file mode 100644 index 28cb90faf..000000000 --- a/lib/scarpe/constants.rb +++ /dev/null @@ -1,24 +0,0 @@ -# frozen_string_literal: true - -require "tmpdir" - -def find_lib_dir - homes = [ - [ENV["LOCALAPPDATA"], "Shoes"], - [ENV["APPDATA"], "Shoes"], - [ENV["HOME"], ".shoes"], - [Dir.tmpdir, "shoes"], - ] - top, file = homes.detect { |home_top, _| home_top && File.exist?(home_top) } - File.join(top, file) -end - -module Constants - LIB_DIR = find_lib_dir - - # Math constants from Shoes3 - RAD2PI = 0.01745329251994329577 - TWO_PI = 6.28318530717958647693 - HALF_PI = 1.57079632679489661923 - PI = 3.14159265358979323846 -end diff --git a/lib/shoes.rb b/lib/shoes.rb new file mode 100644 index 000000000..4b4c51f37 --- /dev/null +++ b/lib/shoes.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +# We're separating Shoes from Scarpe, a little at a time. This should eventually be requirable +# without using Scarpe at all. +# +# This Shoes gem will, if all goes well, be a lot like the old Shoes-core from Shoes4: a way +# to handle the DSL and command-line parts of Shoes without knowing anything about how the +# display side works at all. + +# This will never be triggered -- we use the (...) feature below, which means this +# file won't even parse in old Rubies. +if RUBY_VERSION[0..2] < "3.2" + Scarpe::Logger.logger("Scarpe").error("Scarpe requires Ruby 3.2 or higher!") + exit(-1) +end + +require_relative "shoes/constants" +module Kernel + include Shoes::Constants +end + +class Shoes::Error < StandardError; end + +# The module containing Shoes in all its glory. +# Shoes is a platform-independent GUI library, designed to create +# small visual applications in Ruby. +# +module Shoes + class << self + # Creates a Shoes app with a new window. The block parameter is used to create + # widgets and set up handlers. Arguments are passed to Shoes::App.new internally. + # + # @incompatibility In Shoes3, this method will return normally. + # In Scarpe, after the block is executed, the method will not return and Scarpe + # will retain control of execution until the window is closed and the app quits. + # + # @incompatibility In Shoes3 the parameters were a hash of options, not keyword arguments. + # + # @example Simple one-button app + # Scarpe.app(title: "Button!", width: 200, height: 200) do + # @p = para "Press it NOW!" + # button("clicky") { @p.replace("You pressed it! CELEBRATION!") } + # end + # + # @param title [String] The new app window title + # @param width [Integer] The new app window width + # @param height [Integer] The new app window height + # @param resizable [Boolean] Whether the app window should be resizeable + # @return [void] + # @see Shoes::App#new + def app( + title: "Scarpe!", + width: 480, + height: 420, + resizable: true, + &app_code_body + ) + app = Shoes::App.new(title:, width:, height:, resizable:, &app_code_body) + app.init + app.run + app.destroy + nil + end + end +end diff --git a/lib/shoes/constants.rb b/lib/shoes/constants.rb new file mode 100644 index 000000000..53ed00f2f --- /dev/null +++ b/lib/shoes/constants.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +require "tmpdir" + +module Shoes + module Constants + def self.find_lib_dir + homes = [ + [ENV["LOCALAPPDATA"], "Shoes"], + [ENV["APPDATA"], "Shoes"], + [ENV["HOME"], ".shoes"], + [Dir.tmpdir, "shoes"], + ] + top, file = homes.detect { |home_top, _| home_top && File.exist?(home_top) } + File.join(top, file) + end + + LIB_DIR = find_lib_dir + + # Math constants from Shoes3 + RAD2PI = 0.01745329251994329577 + TWO_PI = 6.28318530717958647693 + HALF_PI = 1.57079632679489661923 + PI = 3.14159265358979323846 + end +end