laymur 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -96,7 +96,6 @@ export declare abstract class UISingleParameterConstraint extends UIConstraint {
96
96
  * @protected
97
97
  */
98
98
  protected isConstraintEnabled(): boolean;
99
- private readonly onWillOrientationChange;
100
99
  /**
101
100
  * Event handler for layer orientation changes.
102
101
  *
package/dist/index.js CHANGED
@@ -1997,12 +1997,22 @@ class UISolverWrapper {
1997
1997
  const description = { variable, priority, value };
1998
1998
  this._private_variables.set(index, description);
1999
1999
  description.constraint = this._private_buildConstraint(new UIExpression(0, [[index, 1]]), new UIExpression(value), UIRelation.EQUAL, priority);
2000
- this._private_solver.addConstraint(description.constraint);
2000
+ try {
2001
+ this._private_solver?.addConstraint(description.constraint);
2002
+ }
2003
+ catch {
2004
+ this._private_solver = undefined;
2005
+ }
2001
2006
  }
2002
2007
  else {
2003
2008
  this._private_variables.set(index, { variable, priority, value });
2004
- this._private_solver.addEditVariable(variable, convertPriority(priority));
2005
- this._private_solver.suggestValue(variable, value);
2009
+ try {
2010
+ this._private_solver?.addEditVariable(variable, convertPriority(priority));
2011
+ this._private_solver?.suggestValue(variable, value);
2012
+ }
2013
+ catch {
2014
+ this._private_solver = undefined;
2015
+ }
2006
2016
  }
2007
2017
  this._private_recalculationRequired = true;
2008
2018
  return index;
@@ -2017,9 +2027,14 @@ class UISolverWrapper {
2017
2027
  throw new Error(`Variable ${index} is used in constraint`);
2018
2028
  }
2019
2029
  }
2020
- description.constraint
2021
- ? this._private_solver.removeConstraint(description.constraint)
2022
- : this._private_solver.removeEditVariable(description.variable);
2030
+ try {
2031
+ description.constraint
2032
+ ? this._private_solver?.removeConstraint(description.constraint)
2033
+ : this._private_solver?.removeEditVariable(description.variable);
2034
+ }
2035
+ catch {
2036
+ this._private_solver = undefined;
2037
+ }
2023
2038
  this._private_variables.delete(index);
2024
2039
  this._private_recalculationRequired = true;
2025
2040
  }
@@ -2031,10 +2046,10 @@ class UISolverWrapper {
2031
2046
  if (description.value !== value) {
2032
2047
  description.value = value;
2033
2048
  if (description.constraint) {
2034
- description.constraint = this._private_rebuildRawConstraint(description.constraint, new UIExpression().plus(index, 1), new UIExpression(description.value), UIRelation.EQUAL, description.priority);
2049
+ description.constraint = this._private_rebuildConstraint(description.constraint, new UIExpression().plus(index, 1), new UIExpression(description.value), UIRelation.EQUAL, description.priority);
2035
2050
  }
2036
2051
  else {
2037
- this._private_solver.suggestValue(description.variable, value);
2052
+ this._private_solver?.suggestValue(description.variable, value);
2038
2053
  }
2039
2054
  this._private_recalculationRequired = true;
2040
2055
  }
@@ -2046,10 +2061,27 @@ class UISolverWrapper {
2046
2061
  }
2047
2062
  if (priority !== description.priority) {
2048
2063
  description.priority = priority;
2049
- if (description.constraint) {
2050
- description.constraint = this._private_rebuildRawConstraint(description.constraint, new UIExpression().plus(index, 1), new UIExpression(description.value), UIRelation.EQUAL, description.priority);
2064
+ if (description.priority === UIPriority.P0) {
2065
+ const lhs = new UIExpression().plus(index, 1);
2066
+ const rhs = new UIExpression(description.value);
2067
+ description.constraint = this._private_buildConstraint(lhs, rhs, UIRelation.EQUAL, description.priority);
2068
+ try {
2069
+ this._private_solver?.addConstraint(description.constraint);
2070
+ }
2071
+ catch {
2072
+ this._private_solver = undefined;
2073
+ }
2051
2074
  }
2052
2075
  else {
2076
+ if (description.constraint) {
2077
+ try {
2078
+ this._private_solver?.removeConstraint(description.constraint);
2079
+ }
2080
+ catch {
2081
+ this._private_solver = undefined;
2082
+ }
2083
+ }
2084
+ description.constraint = undefined;
2053
2085
  this._private_rebuildVariable(index, description);
2054
2086
  }
2055
2087
  this._private_recalculationRequired = true;
@@ -2060,8 +2092,11 @@ class UISolverWrapper {
2060
2092
  if (description === undefined) {
2061
2093
  throw new Error(`Variable ${index} does not exist`);
2062
2094
  }
2095
+ if (this._private_solver === undefined) {
2096
+ this._private_rebuildSolver();
2097
+ }
2063
2098
  if (this._private_recalculationRequired) {
2064
- this._private_solver.updateVariables();
2099
+ this._private_solver?.updateVariables();
2065
2100
  this._private_recalculationRequired = false;
2066
2101
  }
2067
2102
  return description.variable.value();
@@ -2078,7 +2113,12 @@ class UISolverWrapper {
2078
2113
  enabled,
2079
2114
  });
2080
2115
  if (enabled) {
2081
- this._private_solver.addConstraint(constraint);
2116
+ try {
2117
+ this._private_solver?.addConstraint(constraint);
2118
+ }
2119
+ catch {
2120
+ this._private_solver = undefined;
2121
+ }
2082
2122
  this._private_recalculationRequired = true;
2083
2123
  }
2084
2124
  return index;
@@ -2088,7 +2128,14 @@ class UISolverWrapper {
2088
2128
  if (description === undefined) {
2089
2129
  throw new Error(`Constraint ${index} does not exist`);
2090
2130
  }
2091
- this._private_solver.removeConstraint(description.constraint);
2131
+ if (description.enabled) {
2132
+ try {
2133
+ this._private_solver?.removeConstraint(description.constraint);
2134
+ }
2135
+ catch {
2136
+ this._private_solver = undefined;
2137
+ }
2138
+ }
2092
2139
  this._private_constraints.delete(index);
2093
2140
  this._private_recalculationRequired = true;
2094
2141
  }
@@ -2137,99 +2184,96 @@ class UISolverWrapper {
2137
2184
  description.enabled = enabled;
2138
2185
  try {
2139
2186
  description.enabled
2140
- ? this._private_solver.addConstraint(description.constraint)
2141
- : this._private_solver.removeConstraint(description.constraint);
2187
+ ? this._private_solver?.addConstraint(description.constraint)
2188
+ : this._private_solver?.removeConstraint(description.constraint);
2142
2189
  }
2143
2190
  catch {
2144
- this._private_rebuildSolver();
2191
+ this._private_solver = undefined;
2145
2192
  }
2146
2193
  this._private_recalculationRequired = true;
2147
2194
  }
2148
2195
  }
2149
- _private_convertExpression(expression) {
2150
- return new Expression(...expression["prepareTermsInternal"]().map(([variableIndex, coefficient]) => {
2151
- const variable = this._private_variables.get(variableIndex);
2152
- if (variable === undefined) {
2153
- throw new Error(`Variable ${variableIndex} does not exist`);
2154
- }
2155
- return [coefficient, variable.variable];
2156
- }), expression.constant);
2157
- }
2158
2196
  _private_rebuildVariable(index, description) {
2159
- let rebuildRequired = false;
2160
2197
  const dependConstraints = [];
2161
2198
  for (const cDescription of this._private_constraints.values()) {
2162
2199
  if (cDescription.lhs.hasTerm(index) || cDescription.rhs.hasTerm(index)) {
2163
- if (!rebuildRequired) {
2164
- try {
2165
- this._private_solver.removeConstraint(cDescription.constraint);
2166
- }
2167
- catch {
2168
- rebuildRequired = true;
2169
- }
2200
+ try {
2201
+ this._private_solver?.removeConstraint(cDescription.constraint);
2202
+ }
2203
+ catch {
2204
+ this._private_solver = undefined;
2170
2205
  }
2171
2206
  dependConstraints.push(cDescription);
2172
2207
  }
2173
2208
  }
2174
- this._private_solver.removeEditVariable(description.variable);
2175
- this._private_solver.addEditVariable(description.variable, convertPriority(description.priority));
2209
+ this._private_solver?.removeEditVariable(description.variable);
2210
+ this._private_solver?.addEditVariable(description.variable, convertPriority(description.priority));
2176
2211
  for (const cDescription of dependConstraints) {
2177
- cDescription.constraint = new Constraint(this._private_convertExpression(cDescription.lhs), convertRelation(cDescription.relation), this._private_convertExpression(cDescription.rhs), convertPriority(cDescription.priority));
2212
+ cDescription.constraint = this._private_buildConstraint(cDescription.lhs, cDescription.rhs, cDescription.relation, cDescription.priority);
2178
2213
  }
2179
- if (!rebuildRequired) {
2180
- for (const cDescription of dependConstraints) {
2181
- try {
2182
- this._private_solver.addConstraint(cDescription.constraint);
2183
- }
2184
- catch {
2185
- rebuildRequired = true;
2186
- break;
2187
- }
2214
+ for (const cDescription of dependConstraints) {
2215
+ try {
2216
+ this._private_solver?.addConstraint(cDescription.constraint);
2217
+ }
2218
+ catch {
2219
+ this._private_solver = undefined;
2220
+ break;
2188
2221
  }
2189
- }
2190
- if (rebuildRequired) {
2191
- this._private_rebuildSolver();
2192
2222
  }
2193
2223
  }
2194
2224
  _private_rebuildConstraintByDescription(description) {
2195
2225
  description.constraint = description.enabled
2196
- ? this._private_rebuildRawConstraint(description.constraint, description.lhs, description.rhs, description.relation, description.priority)
2226
+ ? this._private_rebuildConstraint(description.constraint, description.lhs, description.rhs, description.relation, description.priority)
2197
2227
  : this._private_buildConstraint(description.lhs, description.rhs, description.relation, description.priority);
2198
2228
  }
2199
- _private_rebuildRawConstraint(oldConstraint, lhs, rhs, relation, priority) {
2200
- let isSolverCrashed = false;
2229
+ _private_rebuildConstraint(oldConstraint, lhs, rhs, relation, priority) {
2201
2230
  try {
2202
- this._private_solver.removeConstraint(oldConstraint);
2231
+ this._private_solver?.removeConstraint(oldConstraint);
2203
2232
  }
2204
2233
  catch {
2205
- isSolverCrashed = true;
2234
+ this._private_solver = undefined;
2206
2235
  }
2207
2236
  const constraint = this._private_buildConstraint(lhs, rhs, relation, priority);
2208
- if (!isSolverCrashed) {
2209
- try {
2210
- this._private_solver.addConstraint(constraint);
2211
- }
2212
- catch {
2213
- isSolverCrashed = true;
2214
- }
2237
+ try {
2238
+ this._private_solver?.addConstraint(constraint);
2215
2239
  }
2216
- if (isSolverCrashed) {
2217
- this._private_rebuildSolver();
2240
+ catch {
2241
+ this._private_solver = undefined;
2218
2242
  }
2219
2243
  return constraint;
2220
2244
  }
2221
2245
  _private_buildConstraint(lhs, rhs, relation, priority) {
2222
2246
  return new Constraint(this._private_convertExpression(lhs), convertRelation(relation), this._private_convertExpression(rhs), convertPriority(priority));
2223
2247
  }
2248
+ _private_convertExpression(expression) {
2249
+ return new Expression(...expression["prepareTermsInternal"]().map(([variableIndex, coefficient]) => {
2250
+ const description = this._private_variables.get(variableIndex);
2251
+ if (description === undefined) {
2252
+ throw new Error(`Variable ${variableIndex} does not exist`);
2253
+ }
2254
+ return [coefficient, description.variable];
2255
+ }), expression.constant);
2256
+ }
2224
2257
  _private_rebuildSolver() {
2258
+ if (this._private_solver) {
2259
+ throw new Error("Solver already exists");
2260
+ }
2225
2261
  this._private_solver = new Solver();
2226
2262
  for (const description of this._private_variables.values()) {
2227
- this._private_solver.addEditVariable(description.variable, description.priority);
2228
- this._private_solver.suggestValue(description.variable, description.value);
2263
+ if (description.constraint) {
2264
+ this._private_solver.addConstraint(description.constraint);
2265
+ }
2266
+ else {
2267
+ this._private_solver.addEditVariable(description.variable, convertPriority(description.priority));
2268
+ this._private_solver.suggestValue(description.variable, description.value);
2269
+ }
2229
2270
  }
2230
2271
  for (const description of this._private_constraints.values()) {
2231
- this._private_solver.addConstraint(description.constraint);
2272
+ if (description.enabled) {
2273
+ this._private_solver.addConstraint(description.constraint);
2274
+ }
2232
2275
  }
2276
+ this._private_recalculationRequired = true;
2233
2277
  }
2234
2278
  }
2235
2279
 
@@ -2238,8 +2282,6 @@ class UISolverWrapper {
2238
2282
  */
2239
2283
  var UILayerEvent;
2240
2284
  (function (UILayerEvent) {
2241
- /** Emitted when the layer's orientation is about to change between horizontal and vertical. */
2242
- UILayerEvent["WILL_ORIENTATION_CHANGE"] = "will_orientation_change";
2243
2285
  /** Emitted when the layer's orientation changes between horizontal and vertical. */
2244
2286
  UILayerEvent["ORIENTATION_CHANGE"] = "orientation_change";
2245
2287
  /** Emitted when the layer's visibility/interaction mode changes. */
@@ -2349,7 +2391,6 @@ class UILayer extends i {
2349
2391
  this.sceneWrapper.resize(width, height);
2350
2392
  const orientation = width > height ? UIOrientation.HORIZONTAL : UIOrientation.VERTICAL;
2351
2393
  if (orientation !== this.orientationInternal) {
2352
- this.emit(UILayerEvent.WILL_ORIENTATION_CHANGE, this.orientationInternal, this);
2353
2394
  this.orientationInternal = orientation;
2354
2395
  this.emit(UILayerEvent.ORIENTATION_CHANGE, this.orientationInternal, this);
2355
2396
  }
@@ -2468,11 +2509,6 @@ class UISingleParameterConstraint extends UIConstraint {
2468
2509
  */
2469
2510
  constructor(layer, priority, relation, orientation) {
2470
2511
  super(layer);
2471
- this._private_onWillOrientationChange = () => {
2472
- if (!this.isConstraintEnabled()) {
2473
- this.solverWrapper.setConstraintEnabled(this.constraint, false);
2474
- }
2475
- };
2476
2512
  /**
2477
2513
  * Event handler for layer orientation changes.
2478
2514
  *
@@ -2482,15 +2518,12 @@ class UISingleParameterConstraint extends UIConstraint {
2482
2518
  * @private
2483
2519
  */
2484
2520
  this._private_onOrientationChange = () => {
2485
- if (this.isConstraintEnabled()) {
2486
- this.solverWrapper.setConstraintEnabled(this.constraint, true);
2487
- }
2521
+ this.solverWrapper.setConstraintEnabled(this.constraint, this.isConstraintEnabled());
2488
2522
  };
2489
2523
  this.priorityInternal = resolvePriority(priority);
2490
2524
  this.relationInternal = resolveRelation(relation);
2491
2525
  this.orientationInternal = resolveOrientation(orientation);
2492
2526
  this.layer.on(UILayerEvent.ORIENTATION_CHANGE, this._private_onOrientationChange);
2493
- this.layer.on(UILayerEvent.WILL_ORIENTATION_CHANGE, this._private_onWillOrientationChange);
2494
2527
  }
2495
2528
  /**
2496
2529
  * Gets the current priority level of the constraint.
@@ -2552,7 +2585,6 @@ class UISingleParameterConstraint extends UIConstraint {
2552
2585
  */
2553
2586
  destroy() {
2554
2587
  this.layer.off(UILayerEvent.ORIENTATION_CHANGE, this._private_onOrientationChange);
2555
- this.layer.off(UILayerEvent.WILL_ORIENTATION_CHANGE, this._private_onWillOrientationChange);
2556
2588
  this.solverWrapper.removeConstraint(this.constraint);
2557
2589
  }
2558
2590
  /**