Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Creating a toggle switch

Posted on 03.03.2013 by Jon Lennart Posted in Developers log, News

Smart Mobile Studio component writing is actually very simple. It’s much easier than under Delphi – but it involves one aspect that to some Spartans can feel.. well, a bit odd. Namely the fact that you have to take CSS into account.

The CSS code for the TW3Switchbox:

.TW3ToggleSwitch {
  background-color: #FFFFFF;
    border:1px solid #AAA;
    border-radius:6px;
    color:#FFF;
    font-weight:700;
    overflow:hidden;
    box-shadow:0 1px 0 #FFF;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
 }
 
.TW3ToggleOnLabel {
  border-radius: 6px;
  text-align:center;
    background:#007FEA;
    text-shadow:0 -1px 0 #093B5C;
    box-shadow:0 4px 5px -2px rgba(0,0,0,0.3) inset;
}
 
.TW3ToggleOffLabel {
  border-radius: 6px;
  text-align:center;
    box-shadow:0 4px 5px -2px rgba(0,0,0,0.3) inset;
}
 
.TW3ToggleKnob {
  cursor: pointer;
  background-color: #FFFFFF;
    border:1px solid #AAA;
    border-radius:4px;
    color:#FFF;
    font-weight:700;
    overflow:hidden;
    box-shadow:0 4px 0 -2px #F1F1F1 inset, 0 2em 2em -2em #AAA inset, 0 0 2px rgba(0,0,0,.5);
}
 
.TW3ToggleKnob:hover {
    background:#E5E5E5;
}

The interface:

TW3ToggleKnob = Class(TW3CustomControl)
protected
  procedure InitializeObject; override;
End;
 
TW3ToggleOnLabel = Class(TW3Label)
public
  class function supportAdjustment:Boolean;override;
end;
 
TW3ToggleOffLabel = Class(TW3Label)
public
  class function supportAdjustment:Boolean;override;
end;
 
TW3ToggleSwitch = Class(TW3CustomControl)
private
  FKnob:    TW3ToggleKnob;
  FOnText:  TW3ToggleOnLabel;
  FOffText: TW3ToggleOffLabel;
  Procedure HandleTouch(Sender: TObject; Info: TW3TouchData);
  procedure HandleMouseDown(sender:Tobject;button:TMouseButton;
            shiftState:TShiftState;x,y:Integer);
  function  getChecked:Boolean;
  procedure setChecked(Const aValue:Boolean);
protected
  procedure InitializeObject; override;
  Procedure FinalizeObject;Override;
  procedure Resize; override;
public
  Property  Checked:Boolean read getChecked write setChecked;
  Procedure Toggle;
End;

The implementation:

//###########################################################################
// TW3ToggleOnLabel
//###########################################################################
 
class function TW3ToggleOnLabel.supportAdjustment:Boolean;
Begin
  result:=False;
end;
 
//###########################################################################
// TW3ToggleOffLabel
//###########################################################################
 
class function TW3ToggleOffLabel.supportAdjustment:Boolean;
Begin
  result:=False;
end;
 
//###########################################################################
// TW3ToggleKnob
//###########################################################################
 
procedure TW3ToggleKnob.InitializeObject;
begin
  inherited;
  w3_setStyle(Handle,w3_CSSPrefix('Transition'),'left .12s');
end;
 
//###########################################################################
// TW3ToggleSwitch
//###########################################################################
 
procedure TW3ToggleSwitch.InitializeObject;
begin
  inherited;
  (* Create the "ON" label *)
  FOnText:=TW3ToggleOnLabel.Create(self);
  FOnText.Caption:='ON';
  FOnText.AlignText:=taCenter;
  FOnText.Font.Color:=clWhite;
 
  (* Create the "OFF" label *)
  FOffText:=TW3ToggleOffLabel.Create(self);
  FOffText.Caption:='OFF';
  FOffText.Font.Color:=clBlack;
  FOffText.AlignText:=taCenter;
 
  (* Create our leaver knob *)
  FKnob:=TW3ToggleKnob.create(self);
 
  (* setup internal handlers *)
  self.OnMouseDown:=HandleMouseDown;
 
  self.OnTouchBegin:=HandleTouch;
end;
 
Procedure TW3ToggleSwitch.FinalizeObject;
Begin
  FKnob.free;
  FOffText.free;
  FOnText.free;
  inherited;
end;
 
function  TW3ToggleSwitch.getChecked:Boolean;
Begin
  result:=FKnob.left>0;
end;
 
procedure TW3ToggleSwitch.setChecked(Const aValue:Boolean);
Begin
  if aValue<>getChecked then
  Toggle;
end;
 
Procedure TW3ToggleSwitch.Toggle;
Begin
  case getChecked of
  True:   FKnob.Left:=0;
  False:  FKnob.Left:=(width div 2)-8;
  end;
  w3_callback(
    procedure ()
    begin
      if assigned(OnChanged) then
      onChanged(self);
    end,
    300);
end;
 
Procedure TW3ToggleSwitch.HandleTouch(Sender: TObject; Info: TW3TouchData);
Begin
  Toggle;
end;
 
procedure TW3ToggleSwitch.HandleMouseDown(sender:TObject;
          button:TMouseButton;
          shiftState:TShiftState;
          x, y : Integer);
Begin
  if button=mbLeft then
  Toggle;
end;
 
procedure TW3ToggleSwitch.Resize;
var
  mTemp:  Integer;
  wd: Integer;
  hd: Integer;
Begin
  wd:=width;
  hd:=height;
 
  if (wd>0) and (hd>0) then
  begin
    mTemp:=(wd div 2);
    if (FKnob.width<>mTemp)
    or (FKnob.height<>hd) then
    FKnob.SetSize(mTemp + 8,hd);
 
    FOnText.SetBounds(0,0,mTemp,hd);
    FOffText.SetBounds(mTemp,0,mTemp,hd);
  end;
end;

The magic movement

In this line we tell the browser that it should apply a transition on a css value, in this case the “left” property of the knob control. So whenever you alter the left position of that control, it will apply a 0.12s delay in the movement.

w3_setStyle(Handle,w3_CSSPrefix('Transition'),'left .12s');
« Smart Mobile Studio 1.1 RC (build 1.1.0.400)
Creating a scrollbar »

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS