Ooui-tws-port/Ooui/Element.cs

135 lines
4.3 KiB
C#

using System;
using System.ComponentModel;
namespace Ooui
{
public abstract class Element : Node
{
string className = "";
public string ClassName {
get => className;
set => SetProperty (ref className, value, "className");
}
public Style Style { get; private set; } = new Style ();
string title = "";
public string Title {
get => title;
set => SetProperty (ref title, value, "title");
}
bool hidden = false;
public bool IsHidden {
get => hidden;
set => SetProperty (ref hidden, value, "hidden");
}
public event TargetEventHandler Click {
add => AddEventListener ("click", value);
remove => RemoveEventListener ("click", value);
}
public event TargetEventHandler DoubleClick {
add => AddEventListener ("dblclick", value);
remove => RemoveEventListener ("dblclick", value);
}
public event TargetEventHandler KeyDown {
add => AddEventListener ("keydown", value);
remove => RemoveEventListener ("keydown", value);
}
public event TargetEventHandler KeyPress {
add => AddEventListener ("keypress", value);
remove => RemoveEventListener ("keypress", value);
}
public event TargetEventHandler KeyUp {
add => AddEventListener ("keyup", value);
remove => RemoveEventListener ("keyup", value);
}
public event TargetEventHandler MouseDown {
add => AddEventListener ("mousedown", value);
remove => RemoveEventListener ("mousedown", value);
}
public event TargetEventHandler MouseEnter {
add => AddEventListener ("mouseenter", value);
remove => RemoveEventListener ("mouseenter", value);
}
public event TargetEventHandler MouseLeave {
add => AddEventListener ("mouseleave", value);
remove => RemoveEventListener ("mouseleave", value);
}
public event TargetEventHandler MouseMove {
add => AddEventListener ("mousemove", value);
remove => RemoveEventListener ("mousemove", value);
}
public event TargetEventHandler MouseOut {
add => AddEventListener ("mouseout", value);
remove => RemoveEventListener ("mouseout", value);
}
public event TargetEventHandler MouseOver {
add => AddEventListener ("mouseover", value);
remove => RemoveEventListener ("mouseover", value);
}
public event TargetEventHandler MouseUp {
add => AddEventListener ("mouseup", value);
remove => RemoveEventListener ("mouseup", value);
}
public event TargetEventHandler Wheel {
add => AddEventListener ("wheel", value);
remove => RemoveEventListener ("wheel", value);
}
/// <summary>
/// A signal to Ooui that this element should take up the
/// entire browser window.
/// </summary>
public virtual bool WantsFullScreen => false;
protected Element (string tagName)
: base (tagName)
{
Style.PropertyChanged += HandleStylePropertyChanged;
}
public void SetAttribute (string attributeName, string value)
{
Send (new Message {
MessageType = MessageType.SetAttribute,
TargetId = Id,
Key = attributeName,
Value = value,
});
}
void HandleStylePropertyChanged (object sender, PropertyChangedEventArgs e)
{
SendSet ("style." + Style.GetJsName (e.PropertyName), Style[e.PropertyName]);
}
protected override bool SaveStateMessageIfNeeded (Message message)
{
if (message.TargetId != Id)
return false;
switch (message.MessageType) {
case MessageType.Call when message.Key.StartsWith ("$.", StringComparison.Ordinal):
AddStateMessage (message);
return true;
default:
return base.SaveStateMessageIfNeeded (message);
}
}
}
}