Ooui-tws-port/Ooui/Element.cs

281 lines
9.3 KiB
C#
Raw Normal View History

2017-06-12 19:09:09 +00:00
using System;
2018-02-02 02:43:23 +00:00
using System.Collections.Generic;
2017-06-18 23:50:22 +00:00
using System.ComponentModel;
2017-06-12 19:09:09 +00:00
2017-06-12 18:45:24 +00:00
namespace Ooui
{
2017-06-12 20:45:27 +00:00
public abstract class Element : Node
2017-06-12 18:45:24 +00:00
{
2018-02-02 02:43:23 +00:00
readonly Dictionary<string, object> attributes = new Dictionary<string, object> ();
2017-06-12 20:45:27 +00:00
public string ClassName {
2018-02-02 02:43:23 +00:00
get => GetStringAttribute ("class", "");
set => SetAttributeProperty ("class", value);
2017-06-13 07:51:24 +00:00
}
2017-06-18 23:50:22 +00:00
public Style Style { get; private set; } = new Style ();
2017-06-13 07:51:24 +00:00
public string Title {
2018-02-02 02:43:23 +00:00
get => GetStringAttribute ("title", "");
set => SetAttributeProperty ("title", value);
2017-06-13 07:51:24 +00:00
}
bool hidden = false;
public bool IsHidden {
2018-02-02 02:43:23 +00:00
get => GetBooleanAttribute ("hidden");
set => SetBooleanAttributeProperty ("hidden", value);
2017-06-12 19:09:09 +00:00
}
2017-06-15 09:39:19 +00:00
public event TargetEventHandler Click {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("click", value);
remove => RemoveEventListener ("click", value);
}
public event TargetEventHandler DoubleClick {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("dblclick", value);
remove => RemoveEventListener ("dblclick", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler KeyDown {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("keydown", value);
remove => RemoveEventListener ("keydown", value);
}
public event TargetEventHandler KeyPress {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("keypress", value);
remove => RemoveEventListener ("keypress", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler KeyUp {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("keyup", value);
remove => RemoveEventListener ("keyup", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler MouseDown {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mousedown", value);
remove => RemoveEventListener ("mousedown", value);
}
public event TargetEventHandler MouseEnter {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mouseenter", value);
remove => RemoveEventListener ("mouseenter", value);
}
public event TargetEventHandler MouseLeave {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mouseleave", value);
remove => RemoveEventListener ("mouseleave", value);
}
public event TargetEventHandler MouseMove {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mousemove", value);
remove => RemoveEventListener ("mousemove", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler MouseOut {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mouseout", value);
remove => RemoveEventListener ("mouseout", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler MouseOver {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mouseover", value);
remove => RemoveEventListener ("mouseover", value);
}
2017-07-08 05:55:04 +00:00
public event TargetEventHandler MouseUp {
2017-06-19 05:28:14 +00:00
add => AddEventListener ("mouseup", value);
remove => RemoveEventListener ("mouseup", value);
}
public event TargetEventHandler Wheel {
2017-06-19 05:28:14 +00:00
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;
2017-06-15 09:39:19 +00:00
protected Element (string tagName)
: base (tagName)
{
2017-06-18 23:50:22 +00:00
Style.PropertyChanged += HandleStylePropertyChanged;
}
2018-02-02 02:43:23 +00:00
protected bool SetAttributeProperty (string attributeName, object newValue, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
var old = GetAttribute (attributeName);
if (old != null && old.Equals (newValue))
return false;
SetAttribute (attributeName, newValue);
OnPropertyChanged (propertyName);
return true;
}
protected bool SetBooleanAttributeProperty (string attributeName, bool newValue, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
var old = GetAttribute (attributeName) != null;
2018-02-03 03:51:35 +00:00
if (old == newValue)
2018-02-02 02:43:23 +00:00
return false;
if (newValue)
SetAttribute (attributeName, string.Empty);
else
RemoveAttribute (attributeName);
OnPropertyChanged (propertyName);
return true;
}
protected bool UpdateAttributeProperty (string attributeName, object newValue, string propertyName)
{
lock (attributes) {
if (attributes.TryGetValue (attributeName, out var oldValue)) {
if (newValue != null && newValue.Equals (oldValue))
return false;
}
attributes[attributeName] = newValue;
}
OnPropertyChanged (propertyName);
return true;
}
protected bool UpdateBooleanAttributeProperty (string attributeName, bool newValue, string propertyName)
{
lock (attributes) {
var oldValue = attributes.ContainsKey (attributeName);
if (newValue == oldValue)
return false;
if (newValue) {
attributes[attributeName] = "";
}
else {
attributes.Remove (attributeName);
}
}
OnPropertyChanged (propertyName);
return true;
}
public void SetAttribute (string attributeName, object value)
2017-11-26 17:28:06 +00:00
{
2018-02-02 02:43:23 +00:00
lock (attributes) {
attributes[attributeName] = value;
}
2017-11-26 17:28:06 +00:00
Send (new Message {
MessageType = MessageType.SetAttribute,
TargetId = Id,
Key = attributeName,
Value = value,
});
}
2018-02-02 02:43:23 +00:00
public object GetAttribute (string attributeName)
{
lock (attributes) {
attributes.TryGetValue (attributeName, out var v);
return v;
}
}
public T GetAttribute<T> (string attributeName, T defaultValue)
{
lock (attributes) {
attributes.TryGetValue (attributeName, out var v);
if (v is T) {
return (T)v;
}
else {
return defaultValue;
}
}
}
public bool GetBooleanAttribute (string attributeName)
{
lock (attributes) {
return attributes.TryGetValue (attributeName, out var _);
}
}
public string GetStringAttribute (string attributeName, string defaultValue)
{
lock (attributes) {
if (attributes.TryGetValue (attributeName, out var v)) {
if (v == null) return "null";
else return v.ToString ();
}
else {
return defaultValue;
}
}
}
public void RemoveAttribute (string attributeName)
{
bool removed;
lock (attributes) {
removed = attributes.Remove (attributeName);
}
if (removed) {
Send (new Message {
MessageType = MessageType.RemoveAttribute,
TargetId = Id,
Key = attributeName,
});
}
}
2017-06-18 23:50:22 +00:00
void HandleStylePropertyChanged (object sender, PropertyChangedEventArgs e)
{
SendSet ("style." + Style.GetJsName (e.PropertyName), Style[e.PropertyName]);
2017-06-15 09:39:19 +00:00
}
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);
}
}
2018-02-02 04:18:16 +00:00
protected virtual bool HtmlNeedsFullEndElement => false;
public override void WriteOuterHtml (System.Xml.XmlWriter w)
{
w.WriteStartElement (TagName);
w.WriteAttributeString ("id", Id);
var style = Style.ToString ();
if (style.Length > 0) {
w.WriteAttributeString ("style", style);
}
lock (attributes) {
foreach (var a in attributes) {
var value = (a.Value == null) ? "null" : Convert.ToString (a.Value, System.Globalization.CultureInfo.InvariantCulture);
w.WriteAttributeString (a.Key, value);
}
}
WriteInnerHtml (w);
if (HtmlNeedsFullEndElement) {
w.WriteFullEndElement ();
}
else {
w.WriteEndElement ();
}
}
public virtual void WriteInnerHtml (System.Xml.XmlWriter w)
{
var children = Children;
foreach (var c in children) {
c.WriteOuterHtml (w);
}
}
2017-06-12 18:45:24 +00:00
}
2017-06-12 19:09:09 +00:00
}